Skip to content

Commit

Permalink
Corrected segmentation fault in python when an invalid annotation is …
Browse files Browse the repository at this point in the history
…being used in some function, add two new APIs to introspect a function signature.
  • Loading branch information
viferga committed Jan 29, 2021
1 parent f210b85 commit 9addeb4
Show file tree
Hide file tree
Showing 7 changed files with 348 additions and 7 deletions.
29 changes: 23 additions & 6 deletions source/loaders/py_loader/source/py_loader_impl.c
Original file line number Diff line number Diff line change
Expand Up @@ -1969,24 +1969,39 @@ int py_loader_impl_clear(loader_impl impl, loader_handle handle)
return 1;
}

type py_loader_impl_discover_type(loader_impl impl, PyObject *annotation)
type py_loader_impl_discover_type(loader_impl impl, PyObject *annotation, const char * func_name, const char * parameter_name)
{
type t = NULL;

if (annotation != NULL)
{
PyObject *annotation_qualname = PyObject_GetAttrString(annotation, "__qualname__");
static const char qualname[] = "__qualname__";

if (PyObject_HasAttrString(annotation, qualname) == 0)
{
if (parameter_name != NULL)
{
log_write("metacall", LOG_LEVEL_WARNING, "Invalid annotation type in the parameter '%s' of the function %s", parameter_name, func_name);
}
else
{
log_write("metacall", LOG_LEVEL_WARNING, "Invalid annotation type in the return type of the function %s", func_name);
}

return NULL;
}

PyObject *annotation_qualname = PyObject_GetAttrString(annotation, qualname);
const char *annotation_name = PyUnicode_AsUTF8(annotation_qualname);

if (strcmp(annotation_name, "_empty") != 0)
{
t = loader_impl_type(impl, annotation_name);

log_write("metacall", LOG_LEVEL_DEBUG, "Discover type (%p) (%p): %s", (void *)annotation, (void *)type_derived(t), annotation_name);

Py_DECREF(annotation_qualname);
}

Py_DECREF(annotation_qualname);
}

return t;
Expand Down Expand Up @@ -2074,6 +2089,8 @@ int py_loader_impl_discover_func(loader_impl impl, PyObject *func, function f)
{
signature s = function_signature(f);

const char * func_name = function_name(f);

PyObject *parameters = PyObject_GetAttrString(result, "parameters");

PyObject *return_annotation = PyObject_GetAttrString(result, "return_annotation");
Expand Down Expand Up @@ -2111,15 +2128,15 @@ int py_loader_impl_discover_func(loader_impl impl, PyObject *func, function f)

PyObject *annotation = PyObject_GetAttrString(parameter, "annotation");

type t = py_loader_impl_discover_type(impl, annotation);
type t = py_loader_impl_discover_type(impl, annotation, func_name, parameter_name);

signature_set(s, iterator, parameter_name, t);
}
}
}
}

signature_set_return(s, py_loader_impl_discover_type(impl, return_annotation));
signature_set_return(s, py_loader_impl_discover_type(impl, return_annotation, func_name, NULL));

return 0;
}
Expand Down
36 changes: 35 additions & 1 deletion source/metacall/include/metacall/metacall.h
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,41 @@ METACALL_API void * metacallt_s(const char * name, const enum metacall_value_id
* @return
* Function reference, null if the function does not exist
*/
METACALL_API void * metacall_function(const char * name);
METACALL_API void * metacall_function(const char * name);

/**
* @brief
* Get the function parameter type id
*
* @param[in] func
* The pointer to the function obtained from metacall_function
*
* @param[in] parameter
* The index of the parameter to be retrieved
*
* @param[out] id
* The parameter type id that will be returned
*
* @return
* Return 0 if the @parameter index exists and @func is valid, 1 otherwhise
*/
METACALL_API int metacall_function_parameter_type(void * func, size_t parameter, enum metacall_value_id * id);

/**
* @brief
* Get the function return type id
*
* @param[in] func
* The pointer to the function obtained from metacall_function
*
*
* @param[out] id
* The value id of the return type of the function @func
*
* @return
* Return 0 if the @func is valid, 1 otherwhise
*/
METACALL_API int metacall_function_return_type(void * func, enum metacall_value_id * id);

/**
* @brief
Expand Down
37 changes: 37 additions & 0 deletions source/metacall/source/metacall.c
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,43 @@ void * metacall_function(const char * name)
}

return f;
}

int metacall_function_parameter_type(void * func, size_t parameter, enum metacall_value_id * id)
{
if (func != NULL)
{
function f = (function)func;
signature s = function_signature(f);

if (parameter < signature_count(s))
{
*id = type_index(signature_get_type(s, parameter));

return 0;
}
}

*id = METACALL_INVALID;

return 1;
}

int metacall_function_return_type(void * func, enum metacall_value_id * id)
{
if (func != NULL)
{
function f = (function)func;
signature s = function_signature(f);

*id = type_index(signature_get_return(s));

return 0;
}

*id = METACALL_INVALID;

return 1;
}

size_t metacall_function_size(void * func)
Expand Down
1 change: 1 addition & 0 deletions source/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ add_subdirectory(metacall_python_varargs_test)
add_subdirectory(metacall_python_port_test)
add_subdirectory(metacall_python_port_https_test)
add_subdirectory(metacall_python_callback_test)
add_subdirectory(metacall_python_fail_test)
add_subdirectory(metacall_map_test)
add_subdirectory(metacall_map_await_test)
add_subdirectory(metacall_initialize_test)
Expand Down
147 changes: 147 additions & 0 deletions source/tests/metacall_python_fail_test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
# Check if this loader is enabled
if(NOT OPTION_BUILD_LOADERS OR NOT OPTION_BUILD_LOADERS_PY)
return()
endif()

#
# Executable name and options
#

# Target name
set(target metacall-python-fail-test)
message(STATUS "Test ${target}")

#
# Compiler warnings
#

include(Warnings)

#
# Compiler security
#

include(SecurityFlags)

#
# Sources
#

set(include_path "${CMAKE_CURRENT_SOURCE_DIR}/include/${target}")
set(source_path "${CMAKE_CURRENT_SOURCE_DIR}/source")

set(sources
${source_path}/main.cpp
${source_path}/metacall_python_fail_test.cpp
)

# Group source files
set(header_group "Header Files (API)")
set(source_group "Source Files")
source_group_by_path(${include_path} "\\\\.h$|\\\\.hpp$"
${header_group} ${headers})
source_group_by_path(${source_path} "\\\\.cpp$|\\\\.c$|\\\\.h$|\\\\.hpp$"
${source_group} ${sources})

#
# Create executable
#

# Build executable
add_executable(${target}
${sources}
)

# Create namespaced alias
add_executable(${META_PROJECT_NAME}::${target} ALIAS ${target})

#
# Project options
#

set_target_properties(${target}
PROPERTIES
${DEFAULT_PROJECT_OPTIONS}
FOLDER "${IDE_FOLDER}"
)

#
# Include directories
#

target_include_directories(${target}
PRIVATE
${DEFAULT_INCLUDE_DIRECTORIES}
${PROJECT_BINARY_DIR}/source/include
)

#
# Libraries
#

target_link_libraries(${target}
PRIVATE
${DEFAULT_LIBRARIES}

GTest

${META_PROJECT_NAME}::metacall_distributable
)

#
# Compile definitions
#

target_compile_definitions(${target}
PRIVATE
${DEFAULT_COMPILE_DEFINITIONS}
)

#
# Compile options
#

target_compile_options(${target}
PRIVATE
${DEFAULT_COMPILE_OPTIONS}
)

#
# Linker options
#

target_link_libraries(${target}
PRIVATE
${DEFAULT_LINKER_OPTIONS}
)

#
# Define test
#

add_test(NAME ${target}
COMMAND $<TARGET_FILE:${target}>
)

#
# Define dependencies
#

add_dependencies(${target}
py_loader
)

#
# Define test properties
#

set_property(TEST ${target}
PROPERTY LABELS ${target}
)

include(TestEnvironmentVariables)

test_environment_variables(${target}
""
${TESTS_ENVIRONMENT_VARIABLES}
)
28 changes: 28 additions & 0 deletions source/tests/metacall_python_fail_test/source/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* MetaCall Library by Parra Studios
* A library for providing a foreign function interface calls.
*
* Copyright (C) 2016 - 2021 Vicente Eduardo Ferrer Garcia <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT 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 <gmock/gmock.h>

int main(int argc, char * argv[])
{
::testing::InitGoogleMock(&argc, argv);

return RUN_ALL_TESTS();
}
Loading

0 comments on commit 9addeb4

Please sign in to comment.