/**
 * * \file stack_protection.c
 * * \brief Module for preventing stack smashing
 * * \author Nikita Kachko (m.kachko@samsung.com)
 * * \version 0.1
 * * \date Created Dec 01, 2016 16:16
 * * \par In Samsung Ukraine R&D Center (SURC) under a contract between
 * * \par LLC "Samsung Electronics Ukraine Company" (Kiev, Ukraine) and
 * * \par "Samsung Elecrtronics Co", Ltd (Seoul, Republic of Korea)
 * * \par Copyright: (c) Samsung Electronics Co, Ltd 2016. All rights reserved.
 * **/

#include <stdint.h>
#include <stdlib.h>
#include "tlStd.h"
#include "TlApi/TlApi.h"
#include "vk_log.h"
#include "stack_protection.h"

#if !defined(VK_EXYNOS_9810) && !defined(VK_MTK_6762) && !defined(VK_MTK_6765)
#ifdef ARM_64
uint64_t __stack_chk_guard = 0;
#else
uint32_t __stack_chk_guard = 0;
#endif
#endif

DECLARE_STACK_PROTECTOR;

void stack_protection_init(void)
{
	int generated_length = sizeof(__stack_chk_guard);
	tlApiRandomGenerateData(TLAPI_ALG_SECURE_RANDOM, &__stack_chk_guard, &generated_length);
}

/*
 * Function that is called when stack corruption detected
 * It is referenced only by a compiler, so no need to declare it in a header
 *
 * In case of specific chipset, already defined into TlEntry.lib.
 *
 */
#if !defined(VK_EXYNOS_9810) && !defined(VK_MTK_6762) && !defined(VK_MTK_6765) && !defined(VK_MTK_6768)
void __stack_chk_fail(void)
{
	LOGE("%s: Stack smashing detected", __func__);
	abort();
}
#endif
