Skip to content
This repository was archived by the owner on Aug 23, 2022. It is now read-only.

Enhance exception tests #618

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
116 changes: 116 additions & 0 deletions tests/integration_tests/src/exceptions_call_chain.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/* TAGS: min cpp exceptions */
/* CC_OPTS: -std=c++14 */
/* LIFT_OPTS: explicit +--explicit_args +--explicit_args_count 8 */
/* LIFT_OPTS: default */
/*
* Copyright (c) 2020 Trail of Bits, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT 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 <stdio.h>
#include <stdlib.h>
#include <stdexcept>

struct CustomException {
char inner_state[128] = "abcdefgh\0";

CustomException(char c) {
inner_state[0] = c;
}

const char *what() {
puts("My inner state is:");
puts(inner_state);
return inner_state;
}

char state() const {
return inner_state[0];
}
};

struct ThrowOnFirstCreation {
static int counter;

ThrowOnFirstCreation() {
if (!counter) {
++counter;
throw std::runtime_error("Counter was zero!");
}
}
};

int ThrowOnFirstCreation::counter = 0;

struct Empty {};

void ThrowRuntime() {
puts("Throwing runtime_error");
throw std::runtime_error("runtime_error");
}

void ThrowOutOfRange() {
puts("Throwing out_of_range");
throw std::out_of_range("out_of_range");
}

void ThrowInt(int a) {
puts("Throwing int");
throw a;
}

void ThrowCustom(char c) {
puts("Throwing custom");
throw CustomException(c);
}

void do_some_throwing(int val) {
int decider = val % 4;
switch (decider) {
case 0: ThrowRuntime();
case 1: ThrowOutOfRange();
case 2: ThrowInt(val);
case 3: ThrowCustom(val);
}
}


void MoreComplexExceptions_impl(int iter, int &val) {
puts("MoreComplexExceptions_impl\n");
int arr[10] = { 0, iter - 2, 2, 3, iter + 3, 5, 6, 7, iter, 9 };
for (int i = 0; i < iter; ++i) {
val += arr[i];
}
try {
do_some_throwing(val);
} catch (...) {
puts("Caught something");
printf("val is %i\n", val);
}
if (iter) {
MoreComplexExceptions_impl(iter - 1, val);
}
}

void MoreComplexExceptions() {
int original_val_location = 0;
MoreComplexExceptions_impl(10, original_val_location);
}

int main(int argc, char *argv[]) {
puts("More complex call chain\n");
MoreComplexExceptions();
puts("End of test");

}
163 changes: 163 additions & 0 deletions tests/integration_tests/src/exceptions_generated_seq.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
/* TAGS: min cpp exceptions */
/* CC_OPTS: -std=c++14 */
/* LIFT_OPTS: explicit +--explicit_args +--explicit_args_count 8 */
/* LIFT_OPTS: default */
/*
* Copyright (c) 2020 Trail of Bits, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT 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 <stdio.h>
#include <stdlib.h>

#include <stdexcept>
#include <type_traits>

struct CustomException {
char inner_state[128] = "abcdefgh\0";

CustomException(char c) {
inner_state[0] = c;
}

const char *what() {
puts("My inner state is:");
puts(inner_state);
return inner_state;
}

char state() const {
return inner_state[0];
}
};

struct Empty {};

void ThrowRuntime() {
puts("Throwing runtime_error");
throw std::runtime_error("runtime_error");
}

void ThrowOutOfRange() {
puts("Throwing out_of_range");
throw std::out_of_range("out_of_range");
}

void ThrowInt(int a) {
puts("Throwing int");
throw a;
}

void ThrowCustom(char c) {
puts("Throwing custom");
throw CustomException(c);
}

void do_some_throwing(int val) {
int decider = val % 4;
switch (decider) {
case 0: ThrowRuntime();
case 1: ThrowOutOfRange();
case 2: ThrowInt(val);
case 3: ThrowCustom(val);
}
}

void do_some_special_throwing(int val) {
if (val == 42) {
puts("Random float is thrown");
throw 4.22f;
}
do_some_throwing(val);
}


#define Ook_def(type) void Ook(const type &err) { \
puts("Ook " #type); \
}

#define Continue_def(type) void continue_print(const type &err) { \
puts("Continue handler for " #type); \
}

#define Continue_branch(head, type) \
if (std::is_same<head, type>::value) { \
puts("Continuing catcher of " #type); \
}

Ook_def(float)
Ook_def(std::runtime_error)
Ook_def(std::out_of_range)
Ook_def(CustomException)
Ook_def(int)
Ook_def(Empty)
Ook_def(std::exception)

template <class Head, class ... Tail>
struct TestRunner {
static void run(int val) {
try {
TestRunner<Tail ...>::run(val);
} catch (Head err) {
Ook(err);
}

Continue_branch(Head, float)
else Continue_branch(Head, std::out_of_range)
else Continue_branch(Head, std::runtime_error)
else Continue_branch(Head, int)
else Continue_branch(Head, CustomException)
else Continue_branch(Head, std::exception)
else Continue_branch(Head, Empty)
}
};

template<typename T>
struct TestRunner<T> {
static void run(int val) {
try {
do_some_special_throwing(val);
} catch (T err) {
Ook(err);
}
}
};

template<typename ...Args>
void RunGeneratedSequences() {
for (int i = 40; i < 50; ++i) {
TestRunner<Args...>::run(i);
puts("\n");
}
}

void GeneratedSequences() {
puts("*** Next sequence");
RunGeneratedSequences<
std::out_of_range, std::runtime_error, int, CustomException, float>();

puts("*** Next sequence");
RunGeneratedSequences<
std::out_of_range, std::runtime_error, int, float, CustomException>();

puts("*** Next sequence");
RunGeneratedSequences<
std::runtime_error, std::out_of_range, std::exception, int, float, CustomException>();
}

int main(int argc, char *argv[]) {
puts("Generated sequences\n");
GeneratedSequences();
puts("End of test");
}
102 changes: 102 additions & 0 deletions tests/integration_tests/src/exceptions_simple.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/* TAGS: min cpp exceptions */
/* CC_OPTS: -std=c++14 */
/* LIFT_OPTS: explicit +--explicit_args +--explicit_args_count 8 */
/* LIFT_OPTS: default */
/*
* Copyright (c) 2020 Trail of Bits, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT 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 <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <stdexcept>

#define CATCH(type, status) catch(type err) { \
puts(#type" was caught " status); \
}

struct CustomException {
char inner_state[128] = "abcdefgh\0";

CustomException(char c) {
inner_state[0] = c;
}

const char *what() {
puts("My inner state is:");
puts(inner_state);
return inner_state;
}

char state() const {
return inner_state[0];
}
};

void ThrowRuntime() {
puts("Throwing runtime_error");
throw std::runtime_error("runtime_error");
}

void ThrowOutOfRange() {
puts("Throwing out_of_range");
throw std::out_of_range("out_of_range");
}

void ThrowInt(int a) {
puts("Throwing int");
throw a;
}

void ThrowCustom(char c) {
puts("Throwing custom");
throw CustomException(c);
}

void SimpleException() {
try {
ThrowRuntime();
} CATCH(std::runtime_error, "OK")

try {
ThrowOutOfRange();
} CATCH(std::runtime_error, "NOK")
CATCH(std::out_of_range, "OK")

try {
ThrowInt(42);
} CATCH(std::runtime_error, "NOK")
CATCH(std::out_of_range, "NOK")
catch (int a) {
if (a != 42) {
puts("Incorrect integer was caught!");
} else {
puts("Correct in was caught!");
}
}

try {
ThrowCustom('m');
} CATCH(std::runtime_error, "NOK")
CATCH(std::out_of_range, "NOK")
CATCH(int, "NOK")
CATCH(CustomException, "OK")
}

int main(int argc, char *argv[]) {
puts("Simple exceptions being thrown\n");
SimpleException();
puts("End of test");
}
Loading