Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: integer overflow in Complexity analysis #2509

Merged
merged 4 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions cmake/SouffleTests.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -388,3 +388,9 @@ function(SOUFFLE_POSITIVE_MULTI_TEST)
FACTS_DIR_NAME ${FACTS_DIR_NAME})
endforeach()
endfunction()

function(SOUFFLE_POSITIVE_FUNCTOR_TEST TEST_NAME)
souffle_run_test_helper(TEST_NAME ${TEST_NAME} FUNCTORS ${ARGN})
souffle_run_test_helper(TEST_NAME ${TEST_NAME} COMPILED FUNCTORS ${ARGN})
souffle_run_test_helper(TEST_NAME ${TEST_NAME} COMPILED_SPLITTED FUNCTORS ${ARGN})
endfunction()
26 changes: 22 additions & 4 deletions src/ram/analysis/Complexity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@

namespace souffle::ram::analysis {

static const int MAX_COMPLEXITY = std::numeric_limits<int>::max();

int ComplexityAnalysis::getComplexity(const Node* node) const {
// visitor
class ValueComplexityVisitor : public Visitor<int> {
Expand All @@ -39,7 +41,12 @@ int ComplexityAnalysis::getComplexity(const Node* node) const {

// conjunction
int visit_(type_identity<Conjunction>, const Conjunction& conj) override {
return dispatch(conj.getLHS()) + dispatch(conj.getRHS());
const int cl = dispatch(conj.getLHS());
const int cr = dispatch(conj.getRHS());
if (cl == MAX_COMPLEXITY || cr == MAX_COMPLEXITY) {
return MAX_COMPLEXITY;
}
return cl + cr;
}

// negation
Expand All @@ -58,11 +65,16 @@ int ComplexityAnalysis::getComplexity(const Node* node) const {
}

int visit_(type_identity<Constraint>, const Constraint& c) override {
return dispatch(c.getLHS()) + dispatch(c.getRHS());
const int cl = dispatch(c.getLHS());
const int cr = dispatch(c.getRHS());
if (cl == MAX_COMPLEXITY || cr == MAX_COMPLEXITY) {
return MAX_COMPLEXITY;
}
return cl + cr;
}

int visit_(type_identity<UserDefinedOperator>, const UserDefinedOperator&) override {
return std::numeric_limits<int>::max();
return MAX_COMPLEXITY;
}

// emptiness check
Expand All @@ -74,7 +86,13 @@ int ComplexityAnalysis::getComplexity(const Node* node) const {
int visit_(type_identity<AbstractOperator>, const AbstractOperator& op) override {
int exprComplexity = 0;
for (auto* expr : op.getArguments()) {
exprComplexity += dispatch(*expr);
const int complexity = dispatch(*expr);
if (complexity == MAX_COMPLEXITY) {
exprComplexity = MAX_COMPLEXITY;
break;
} else {
exprComplexity += dispatch(*expr);
}
}
return exprComplexity;
}
Expand Down
3 changes: 3 additions & 0 deletions tests/evaluation/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,6 @@ positive_test(unsigned_operations)
positive_test(unused_constraints)
positive_test(x9)
positive_test(issue2160)

add_subdirectory(issue2508)
souffle_positive_functor_test(issue2508 CATEGORY evaluation)
1 change: 1 addition & 0 deletions tests/evaluation/issue2508/B.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
()
1 change: 1 addition & 0 deletions tests/evaluation/issue2508/C.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
()
28 changes: 28 additions & 0 deletions tests/evaluation/issue2508/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Souffle - A Datalog Compiler
# Copyright (c) 2022 The Souffle Developers. All rights reserved
# Licensed under the Universal Permissive License v 1.0 as shown at:
# - https://opensource.org/licenses/UPL
# - <souffle root>/licenses/SOUFFLE-UPL.txt

add_library(issue2508 SHARED functors.cpp)
target_include_directories(issue2508 PRIVATE "${CMAKE_SOURCE_DIR}/src/include")

target_compile_features(issue2508
PUBLIC cxx_std_17)

set_target_properties(issue2508 PROPERTIES CXX_EXTENSIONS OFF)
set_target_properties(issue2508 PROPERTIES OUTPUT_NAME "functors")
set_target_properties(issue2508 PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS ON)

if (WIN32)
# Prefix all shared libraries with 'lib'.
set(CMAKE_SHARED_LIBRARY_PREFIX "lib")

# Prefix all static libraries with 'lib'.
set(CMAKE_STATIC_LIBRARY_PREFIX "lib")
endif ()

if (SOUFFLE_DOMAIN_64BIT)
target_compile_definitions(issue2508
PUBLIC RAM_DOMAIN_SIZE=64)
endif()
16 changes: 16 additions & 0 deletions tests/evaluation/issue2508/functors.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include "souffle/RecordTable.h"
#include "souffle/SymbolTable.h"

extern "C" {

souffle::RamDomain id(
souffle::SymbolTable* symbolTable, souffle::RecordTable* recordTable, souffle::RamDomain x) {
return x;
}

souffle::RamDomain decode(
souffle::SymbolTable* symbolTable, souffle::RecordTable* recordTable, souffle::RamDomain x) {
symbolTable->decode(x);
return x;
}
}
22 changes: 22 additions & 0 deletions tests/evaluation/issue2508/issue2508.dl
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
.functor id(symbol): symbol stateful
.functor decode(symbol): symbol stateful

.type T = C1 {A0 : symbol} | C2 {A0: number}

.decl A(A2: T)
.decl B()
.output B()
.decl C()
.output C()

A($C2(10000000)).
A($C1("X")).
A($C1("Y")).

B() :-
A($C1(X)),
(@decode(X) = @id("Y")).

C() :-
A($C2(_)).

Empty file.
Empty file.
7 changes: 0 additions & 7 deletions tests/interface/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,6 @@ add_subdirectory(lattice1)
add_subdirectory(lattice2)
add_subdirectory(lattice3)

# swig test which will run python, java or both
function(SOUFFLE_POSITIVE_FUNCTOR_TEST TEST_NAME)
souffle_run_test_helper(TEST_NAME ${TEST_NAME} FUNCTORS ${ARGN})
souffle_run_test_helper(TEST_NAME ${TEST_NAME} COMPILED FUNCTORS ${ARGN})
souffle_run_test_helper(TEST_NAME ${TEST_NAME} COMPILED_SPLITTED FUNCTORS ${ARGN})
endfunction()

function(SOUFFLE_RUN_CPP_TEST)
cmake_parse_arguments(
PARAM
Expand Down
5 changes: 0 additions & 5 deletions tests/semantic/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,6 @@ function(negative_test NAME)
souffle_negative_test(${NAME} semantic)
endfunction()

function(SOUFFLE_POSITIVE_FUNCTOR_TEST TEST_NAME)
souffle_run_test_helper(TEST_NAME ${TEST_NAME} FUNCTORS ${ARGN})
souffle_run_test_helper(TEST_NAME ${TEST_NAME} COMPILED FUNCTORS ${ARGN})
endfunction()

negative_test(adt_invalid_arity)
negative_test(adt_invalid_branch)
negative_test(agg_checks)
Expand Down
Loading