2019-11-06 12:36:56 +00:00
|
|
|
#if 0
|
2019-08-31 21:39:17 +00:00
|
|
|
|
|
|
|
#include <Functions/IFunction.h>
|
|
|
|
#include <Functions/FunctionFactory.h>
|
|
|
|
#include <Functions/FunctionHelpers.h>
|
|
|
|
#include <DataTypes/DataTypeString.h>
|
|
|
|
#include <DataTypes/DataTypesNumber.h>
|
|
|
|
#include <Columns/ColumnString.h>
|
2019-11-06 11:17:12 +00:00
|
|
|
#include <Interpreters/Context.h>
|
2021-10-02 07:13:14 +00:00
|
|
|
#include <base/scope_guard.h>
|
2019-08-31 21:39:17 +00:00
|
|
|
|
|
|
|
#include <thread>
|
|
|
|
#include <memory>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <unistd.h>
|
2020-05-30 20:02:44 +00:00
|
|
|
#include <sys/mman.h>
|
2019-08-31 21:39:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int ILLEGAL_COLUMN;
|
|
|
|
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
|
|
|
|
extern const int BAD_ARGUMENTS;
|
2020-05-30 20:45:30 +00:00
|
|
|
extern const int CANNOT_ALLOCATE_MEMORY;
|
2019-08-31 21:39:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Various illegal actions to test diagnostic features of ClickHouse itself. Should not be enabled in production builds.
|
|
|
|
class FunctionTrap : public IFunction
|
|
|
|
{
|
2019-11-06 11:17:12 +00:00
|
|
|
private:
|
2021-06-01 12:20:52 +00:00
|
|
|
ContextPtr context;
|
2019-11-06 11:17:12 +00:00
|
|
|
|
2019-08-31 21:39:17 +00:00
|
|
|
public:
|
|
|
|
static constexpr auto name = "trap";
|
2021-06-01 12:20:52 +00:00
|
|
|
static FunctionPtr create(ContextPtr context)
|
2019-08-31 21:39:17 +00:00
|
|
|
{
|
2019-11-06 11:17:12 +00:00
|
|
|
return std::make_shared<FunctionTrap>(context);
|
2019-08-31 21:39:17 +00:00
|
|
|
}
|
|
|
|
|
2021-06-01 12:20:52 +00:00
|
|
|
FunctionTrap(ContextPtr context_) : context(context_) {}
|
2019-11-06 11:17:12 +00:00
|
|
|
|
2019-08-31 21:39:17 +00:00
|
|
|
String getName() const override
|
|
|
|
{
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t getNumberOfArguments() const override
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-06-22 16:21:23 +00:00
|
|
|
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return false; }
|
2021-04-29 14:48:26 +00:00
|
|
|
|
2019-08-31 21:39:17 +00:00
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
|
|
|
|
{
|
|
|
|
if (!isString(arguments[0]))
|
|
|
|
throw Exception("The only argument for function " + getName() + " must be constant String", ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
|
|
|
|
return std::make_shared<DataTypeUInt8>();
|
|
|
|
}
|
|
|
|
|
2020-12-17 03:45:46 +00:00
|
|
|
[[clang::optnone]]
|
|
|
|
ColumnPtr executeImpl(const ColumnsWithTypeAndName & block, const DataTypePtr & result_type, size_t input_rows_count) const override
|
2019-08-31 21:39:17 +00:00
|
|
|
{
|
2020-12-17 03:45:46 +00:00
|
|
|
if (const ColumnConst * column = checkAndGetColumnConst<ColumnString>(block[0].column.get()))
|
2019-08-31 21:39:17 +00:00
|
|
|
{
|
|
|
|
String mode = column->getValue<String>();
|
|
|
|
|
|
|
|
if (mode == "read nullptr c++")
|
|
|
|
{
|
|
|
|
volatile int x = *reinterpret_cast<const volatile int *>(0);
|
|
|
|
(void)x;
|
|
|
|
}
|
|
|
|
else if (mode == "read nullptr asm")
|
|
|
|
{
|
|
|
|
__asm__ volatile ("movq $0, %rax");
|
|
|
|
__asm__ volatile ("movq (%rax), %rax");
|
|
|
|
}
|
|
|
|
else if (mode == "illegal instruction")
|
|
|
|
{
|
|
|
|
__asm__ volatile ("ud2a");
|
|
|
|
}
|
|
|
|
else if (mode == "abort")
|
|
|
|
{
|
|
|
|
abort();
|
|
|
|
}
|
2019-12-23 20:26:46 +00:00
|
|
|
else if (mode == "std::terminate")
|
|
|
|
{
|
|
|
|
std::terminate();
|
|
|
|
}
|
2019-08-31 21:39:17 +00:00
|
|
|
else if (mode == "use after free")
|
|
|
|
{
|
|
|
|
int * x_ptr;
|
|
|
|
{
|
|
|
|
auto x = std::make_unique<int>();
|
|
|
|
x_ptr = x.get();
|
|
|
|
}
|
|
|
|
*x_ptr = 1;
|
|
|
|
(void)x_ptr;
|
|
|
|
}
|
|
|
|
else if (mode == "use after scope")
|
|
|
|
{
|
|
|
|
volatile int * x_ptr;
|
|
|
|
[&]{
|
|
|
|
volatile int x = 0;
|
|
|
|
x_ptr = &x;
|
|
|
|
(void)x;
|
|
|
|
}();
|
|
|
|
[&]{
|
|
|
|
volatile int y = 1;
|
|
|
|
*x_ptr = 2;
|
|
|
|
(void)y;
|
|
|
|
}();
|
|
|
|
(void)x_ptr;
|
|
|
|
}
|
|
|
|
else if (mode == "uninitialized memory")
|
|
|
|
{
|
|
|
|
int x;
|
|
|
|
(void)write(2, &x, sizeof(x));
|
|
|
|
}
|
|
|
|
else if (mode == "data race")
|
|
|
|
{
|
|
|
|
int x = 0;
|
|
|
|
std::thread t1([&]{ ++x; });
|
|
|
|
std::thread t2([&]{ ++x; });
|
|
|
|
t1.join();
|
|
|
|
t2.join();
|
|
|
|
}
|
2020-01-02 07:31:25 +00:00
|
|
|
else if (mode == "throw exception")
|
|
|
|
{
|
|
|
|
std::vector<int>().at(0);
|
|
|
|
}
|
2019-11-06 11:17:12 +00:00
|
|
|
else if (mode == "access context")
|
|
|
|
{
|
|
|
|
(void)context.getCurrentQueryId();
|
|
|
|
}
|
2020-12-17 03:45:46 +00:00
|
|
|
else if (mode == "stack overflow")
|
|
|
|
{
|
|
|
|
executeImpl(block, result_type, input_rows_count);
|
|
|
|
}
|
|
|
|
else if (mode == "harmful function")
|
|
|
|
{
|
|
|
|
double res = drand48();
|
|
|
|
(void)res;
|
|
|
|
}
|
2020-05-30 20:02:44 +00:00
|
|
|
else if (mode == "mmap many")
|
|
|
|
{
|
|
|
|
std::vector<void *> maps;
|
|
|
|
SCOPE_EXIT(
|
|
|
|
{
|
|
|
|
//for (void * map : maps)
|
|
|
|
// munmap(map, 4096);
|
|
|
|
});
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
void * hint = reinterpret_cast<void *>(
|
|
|
|
std::uniform_int_distribution<intptr_t>(0x100000000000UL, 0x700000000000UL)(thread_local_rng));
|
|
|
|
void * map = mmap(hint, 4096, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
|
|
|
|
if (MAP_FAILED == map)
|
|
|
|
throwFromErrno("Allocator: Cannot mmap", ErrorCodes::CANNOT_ALLOCATE_MEMORY);
|
|
|
|
maps.push_back(map);
|
|
|
|
}
|
|
|
|
}
|
2019-08-31 21:39:17 +00:00
|
|
|
else
|
|
|
|
throw Exception("Unknown trap mode", ErrorCodes::BAD_ARGUMENTS);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
throw Exception("The only argument for function " + getName() + " must be constant String", ErrorCodes::ILLEGAL_COLUMN);
|
|
|
|
|
2020-12-17 03:45:46 +00:00
|
|
|
return result_type->createColumnConst(input_rows_count, 0ULL);
|
2019-08-31 21:39:17 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
void registerFunctionTrap(FunctionFactory & factory)
|
|
|
|
{
|
|
|
|
factory.registerFunction<FunctionTrap>();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
class FunctionFactory;
|
|
|
|
void registerFunctionTrap(FunctionFactory &) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|