threadsafe stack

/****************************************************************************
@File Name: safestack.h
@Author: wangzhicheng
@mail: 2363702560@qq.com
@Created Time: Wed 08 Mar 2017 09:06:48 AM CST
****************************************************************************/
#include <atomic>
#include <memory>
#include <mutex>
#include <stack>
namespace safestack
{
using namespace std;
template<typename T>
class SafeStack
{
private:
	const int m_nlimit;
	atomic_int m_total;
	stack<T>m_data;
	mutable mutex m_mutex;
public:
	SafeStack(int n = 1000):m_nlimit(n)
	{
		m_total = 0;
	}
	SafeStack(const SafeStack &other)
	{
		lock_guard<mutex>lock(other.m_mutex);
		m_data = other.data;
	}
	SafeStack & operator =(const SafeStack&) = delete; 
	bool push(T value)
	{
		if(m_total >= m_nlimit) return false;
		++m_total;
		lock_guard<mutex>lock(m_mutex);
		m_data.push(value);

		return true;
	}
	shared_ptr<T>pop()
	{
		lock_guard<mutex>lock(m_mutex);
		if(m_data.empty()) return nullptr;
		--m_total;
		shared_ptr<T>const res(make_shared<T>(m_data.top()));
		m_data.pop();

		return res;
	}
	bool pop(T &value)
	{
		lock_guard<mutex>lock(m_mutex);
		if(m_data.empty()) return false;
		--m_total;
		value = m_data.top();
		m_data.pop();

		return true;
	}
	bool empty() const
	{
		lock_guard<mutex>lock(m_mutex);
		return m_data.empty();
	}
};
}

/****************************************************************************
@File Name: test.cpp
@Author: wangzhicheng
@mail: 2363702560@qq.com
@Created Time: Wed 08 Mar 2017 09:09:30 AM CST
****************************************************************************/
#include "safestack.h"
#include <iostream>
#include <thread>
using namespace safestack;
static const int N = 1000000;
SafeStack<int>ss(N);
void thread_push()
{
	while(1)
	{
		for(int i = 0;i < N;i++) ss.push(i);
	}
}
void thread_pop()
{
	while(1)
	{
		shared_ptr<int>ptr = ss.pop();
		if(!ptr) cout << "stack is empty...!" << endl;
	}
}
int main()
{
	thread th0(thread_push);
	thread th1(thread_pop);
	th0.join();
	th1.join();

	return 0;
}

CC=g++
all:
	$(CC) -std=c++11 -g -o safestackTest test.cpp safestack.h -pthread

`__stack_chk_fail_local` 是一个与堆栈保护机制相关的运行时错误,通常出现在程序检测到堆栈溢出或内存破坏时。该机制是编译器在生成代码时插入的一种安全检查手段,用于防止缓冲区溢出攻击,从而提升程序的安全性。以下是对该错误的详细解释以及可能的修复方法。 ### 堆栈保护机制概述 现代编译器(如 GCC 和 Clang)通常默认启用堆栈保护功能。堆栈保护机制的核心思想是在函数的堆栈帧中插入一个“金丝雀值”(canary value),并在函数返回前检查该值是否被修改。如果检测到金丝雀值被更改,程序会调用 `__stack_chk_fail_local` 函数,触发错误并终止程序,防止潜在的安全漏洞被利用。 金丝雀值通常是一个随机生成的数值,在程序启动时初始化,并在每次函数调用时被放置在堆栈上。如果由于缓冲区溢出或其他内存操作错误导致金丝雀值被覆盖,程序会在函数返回时检测到这一变化并采取措施。 ### 错误原因 `__stack_chk_fail_local` 错误的常见原因包括: 1. **缓冲区溢出**:在函数中使用固定大小的数组时,如果写入的数据超出了数组的边界,可能会覆盖金丝雀值。例如,使用 `strcpy` 函数将过长的字符串复制到固定大小的字符数组中。 2. **内存破坏**:不正确的指针操作可能导致意外修改堆栈上的数据,包括金丝雀值。 3. **多线程问题**:在多线程环境中,线程局部存储(Thread Local Storage, TLS)的错误使用可能导致堆栈保护机制失效。 4. **编译器优化问题**:某些编译器优化选项可能会影响堆栈保护机制的正确性,尤其是在复杂的代码结构中。 ### 修复方法 1. **检查缓冲区溢出**:仔细检查代码中涉及数组和字符串操作的部分,确保不会超出数组的边界。可以使用更安全的函数替代传统的不安全函数。例如,使用 `strncpy` 替代 `strcpy`,或者使用 `snprintf` 替代 `sprintf`。 ```c char buffer[10]; strncpy(buffer, input, sizeof(buffer) - 1); buffer[sizeof(buffer) - 1] = '\0'; ``` 2. **使用动态分配**:对于需要处理可变长度数据的情况,可以考虑使用动态分配的内存,而不是固定大小的数组。 3. **启用编译器警告**:启用编译器的警告选项,例如 `-Wall` 和 `-Wextra`,可以帮助发现潜在的缓冲区溢出问题。 4. **禁用堆栈保护(不推荐)**:在某些情况下,可以通过编译器选项禁用堆栈保护机制,但这会降低程序的安全性。例如,使用 `-fno-stack-protector` 选项禁用堆栈保护。 ```bash gcc -fno-stack-protector -o prog prog.c ``` 5. **调试和分析工具**:使用调试工具(如 GDB)和静态分析工具可以帮助定位堆栈溢出的具体位置。通过调试器设置断点并检查堆栈帧的状态,可以更直观地发现问题。 6. **更新编译器和库**:确保使用的编译器和库是最新的版本,以避免已知的堆栈保护相关问题。 ### 示例代码 以下是一个简单的示例,展示了如何避免缓冲区溢出问题: ```c #include <stdio.h> #include <string.h> void safe_copy(const char *input) { char buffer[10]; // 使用 strncpy 避免缓冲区溢出 strncpy(buffer, input, sizeof(buffer) - 1); buffer[sizeof(buffer) - 1] = '\0'; // 确保字符串以 null 结尾 printf("Copied: %s\n", buffer); } int main() { const char *input = "This is a long string that will cause overflow"; safe_copy(input); return 0; } ``` 在上述代码中,`strncpy` 函数用于限制复制的字节数,从而避免缓冲区溢出。此外,手动添加 null 终止符确保字符串的完整性。 ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值