Skip to content

Commit

Permalink
Fix uninitialized access. (#1405)
Browse files Browse the repository at this point in the history
Also add a valgrind test to the buildbot.
  • Loading branch information
floitsch authored Feb 7, 2023
1 parent d07bbe4 commit 42ddf0d
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ jobs:
if: runner.os == 'Linux'
run: |
sudo apt-get update -q
sudo apt-get install -q ninja-build ccache gcc-multilib g++-multilib socat
sudo apt-get install -q ninja-build ccache gcc-multilib g++-multilib socat valgrind
ninja --version
cmake --version
gcc --version
Expand Down
2 changes: 1 addition & 1 deletion src/messaging.h
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ class MessageEncoder {
// someone else has taken responsibility for it and the data
// it points at.
uint8* buffer_;
bool take_ownership_of_buffer_;
bool take_ownership_of_buffer_ = false;
int cursor_ = 0;
int nesting_ = 0;
int problematic_class_id_ = -1;
Expand Down
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,4 @@ add_subdirectory(fuzzer)
add_subdirectory(cmake)
add_subdirectory(lock_file)
add_subdirectory(vessels)
add_subdirectory(valgrind)
63 changes: 63 additions & 0 deletions tests/valgrind/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Copyright (C) 2023 Toitware ApS.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; version
# 2.1 only.
#
# This library 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
# Lesser General Public License for more details.
#
# The license can be found in the file `LICENSE` in the top level
# directory of this repository.

# Valgrind only works on Linux.
# Return if we are on any other platform.
if(NOT CMAKE_SYSTEM_NAME STREQUAL "Linux")
return()
endif()

file(GLOB VALGRIND_TESTS RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" "*_test.toit")

set(VALGRIND_TEST_DIR ${CMAKE_BINARY_DIR}/valgrind_test)

include("${TOIT_SDK_SOURCE_DIR}/tools/toit.cmake")

set(TOITP_SOURCE "${TOIT_SDK_SOURCE_DIR}/tools/toitp.toit")
set(TOITP_EXE "${CMAKE_CURRENT_BINARY_DIR}/toitp${CMAKE_EXECUTABLE_SUFFIX}")
set(TOITP_DEP "${CMAKE_CURRENT_BINARY_DIR}/toitp.dep")

set(CTEST_DIR ${CMAKE_BINARY_DIR}/ctest)

include(fail.cmake OPTIONAL)

foreach(file ${VALGRIND_TESTS})
file(RELATIVE_PATH test_name ${PROJECT_SOURCE_DIR} "${CMAKE_CURRENT_SOURCE_DIR}/${file}")

if("${test_name}" IN_LIST TOIT_SKIP_TESTS)
continue()
endif()

set(input "${CMAKE_CURRENT_SOURCE_DIR}/${file}")
set(snap ${VALGRIND_TEST_DIR}/${file}.snap)

set(valgrind_xml_prefix ${VALGRIND_TEST_DIR}/${file}.valgrind)

add_test(
NAME ${test_name}
COMMAND ${CMAKE_COMMAND}
-DTOIT_RUN=$<TARGET_FILE:toit.run>
-DTOIT_COMPILE=$<TARGET_FILE:toit.compile>
"-DINPUT=${input}"
"-DSNAPSHOT=${snap}"
"-DVALGRIND_XML_PREFIX=${valgrind_xml_prefix}"
-P "${CMAKE_CURRENT_SOURCE_DIR}/run.cmake"
)
set_tests_properties(${test_name} PROPERTIES TIMEOUT 40)

if("${test_name}" IN_LIST TOIT_FAILING_TESTS)
set_tests_properties(${test_name} PROPERTIES WILL_FAIL TRUE)
endif()
endforeach()
6 changes: 6 additions & 0 deletions tests/valgrind/hello_world_test.toit
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Copyright (C) 2023 Toitware ApS.
// Use of this source code is governed by a Zero-Clause BSD license that can
// be found in the tests/LICENSE file.
main:
print "hello world"
86 changes: 86 additions & 0 deletions tests/valgrind/run.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# Copyright (C) 2023 Toitware ApS.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; version
# 2.1 only.
#
# This library 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
# Lesser General Public License for more details.
#
# The license can be found in the file `LICENSE` in the top level
# directory of this repository.

if (NOT DEFINED TOIT_COMPILE)
message(FATAL_ERROR "Missing TOIT_COMPILE argument")
endif()
if (NOT DEFINED TOIT_RUN)
message(FATAL_ERROR "Missing TOIT_RUN argument")
endif()
if (NOT DEFINED INPUT)
message(FATAL_ERROR "Missing INPUT argument")
endif()
if (NOT DEFINED SNAPSHOT)
message(FATAL_ERROR "Missing SNAPSHOT argument")
endif()
if (NOT DEFINED VALGRIND_XML_PREFIX)
message(FATAL_ERROR "Missing VALGRIND_XML_PREFIX argument")
endif()

# TODO: make this configurable.
find_program("VALGRIND" "valgrind")
if (NOT VALGRIND)
message(FATAL_ERROR "Missing valgrind")
endif()

function(backtick)
message("Running command " ${ARGN})
execute_process(
COMMAND ${ARGN}
#COMMAND_ERROR_IS_FATAL ANY
)
endfunction()

# Make sure the directory for the XML files exists.
get_filename_component(VALGRIND_XML_DIR ${VALGRIND_XML_PREFIX} DIRECTORY)
file(MAKE_DIRECTORY ${VALGRIND_XML_DIR})

set(VALGRIND_COMPILE_XML "${VALGRIND_XML_PREFIX}-compile.xml")
backtick(
"${VALGRIND}"
"--xml=yes" "--xml-file=${VALGRIND_COMPILE_XML}"
"--show-leak-kinds=none" # No leak check for the compilation.
"${TOIT_COMPILE}" "-w" "${SNAPSHOT}" "${INPUT}"
)

set(VALGRIND_RUN_XML "${VALGRIND_XML_PREFIX}-run.xml")
backtick(
"${VALGRIND}"
"--xml=yes" "--xml-file=${VALGRIND_RUN_XML}"
# TODO(florian): enable leak detection for the run.
"--show-leak-kinds=none"
"${TOIT_RUN}" "${SNAPSHOT}"
)

set(ERRORS_DETECTED FALSE)
function(check_valgrind_errors xml_file)
file(READ ${xml_file} VALGRIND_OUTPUT)

# Extract all lines of the form '<kind>...</kind>'.
string(REGEX MATCHALL "<kind>[^<]*</kind>" VALGRIND_ERRORS "${VALGRIND_OUTPUT}")

# If we have a line that is not a leak, fail.
if (VALGRIND_ERRORS)
set(ERRORS_DETECTED TRUE)
message("Valgrind errors in ${xml_file}: ${VALGRIND_ERRORS}")
endif()
endfunction()

check_valgrind_errors(${VALGRIND_COMPILE_XML})
check_valgrind_errors(${VALGRIND_RUN_XML})

if (ERRORS_DETECTED)
message(FATAL_ERROR "Valgrind errors detected")
endif()

0 comments on commit 42ddf0d

Please sign in to comment.