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

gtest下mock类成员函数运行时coredump #12

Closed
ROBTTO opened this issue Sep 18, 2020 · 4 comments
Closed

gtest下mock类成员函数运行时coredump #12

ROBTTO opened this issue Sep 18, 2020 · 4 comments

Comments

@ROBTTO
Copy link

ROBTTO commented Sep 18, 2020

#include<gtest/gtest.h>

#include <emock/emock.hpp>

class Foo
{
public:
	int test(double)
	{
		return 1;
	}
};

TEST(test, testcase)
{
	EMOCK(
		&Foo::test)
		.stubs()
		.with(any())
		.will(returnValue(2));
	Foo foo;
	EXPECT_EQ(2, foo.test(4));
}

int main(int argc, char **argv)
{
	testing::InitGoogleTest(&argc, argv);
	return RUN_ALL_TESTS();
}

image

@orca-zhang
Copy link
Member

看起来用法没有问题,猜测是不是和机型有关,触发的bug,是64位的吗
可以加上调试信息确认一下挂在哪一行不?现在只能看到是在获取蹦床的时候出的错

@ROBTTO
Copy link
Author

ROBTTO commented Sep 21, 2020

看起来用法没有问题,猜测是不是和机型有关,触发的bug,是64位的吗
可以加上调试信息确认一下挂在哪一行不?现在只能看到是在获取蹦床的时候出的错

$: uname -a
Linux K 5.4.0-45-generic #49~18.04.2-Ubuntu SMP Wed Aug 26 16:29:02 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux

$: cat /etc/issue
Ubuntu 18.04.5 LTS \n \l

64位机器
普通函数mock是没有问题的,就成员函数mock时会coredump

TrampolineAllocate函数返回了空指针造成coredump

    void* TrampolineAllocate(const unsigned char* dst, size_t alloc_size)
    {
        FILE* fp = fopen("/proc/self/maps", "r");
        if(!fp) {
            EMOCK_REPORT_FAILURE("Failed to fetch current proc maps");
            return NULL;
        }

        unsigned long last_end = 0;
        while(!feof(fp)) {
            char buf[PATH_MAX + 100] = {0};
            if(fgets(buf, sizeof(buf), fp) == 0)
                break;

            unsigned long begin, end = 0;
            sscanf(buf, "%lx-%lx %*[^\n]", &begin, &end);
            if(last_end && begin != last_end && begin - last_end > alloc_size) {
                // alloc at end of last
                if((size_t)(dst - (unsigned char*)last_end) < kMaxAllocationDelta) {              // 条件不满足
                    if(void* allocated = TrampolineAllocateImpl(dst, alloc_size)) {
                        fclose(fp);
                        return allocated;
                    }
                }
                // alloc at begin of current
                if((size_t)((unsigned char*)begin - dst) < kMaxAllocationDelta) {                  // 条件不满足
                    if(void* allocated = TrampolineAllocateImpl(dst - alloc_size, alloc_size)) {
                        fclose(fp);
                        return allocated;
                    }
                }
            }
            last_end = end;
        }

        fclose(fp);
        return NULL;
    }

循环中两个判断if((size_t)(dst - (unsigned char*)last_end) < kMaxAllocationDelta) if((size_t)((unsigned char*)begin - dst) < kMaxAllocationDelta) 都不满足,最后直接break返回NULL

@orca-zhang
Copy link
Member

是最新代码编译的对吧,最新有一次commit修复了之前last_end错误初始化的问题
应该是当前内存页内没有找到跳板,这个case实在不行只能用longjump覆盖了,但demo里面的成员函数确实不够13字节,存在不安全的可能性

@orca-zhang
Copy link
Member

@slllovehaski 应该修复了这个问题,确实是跳板申请的位置不对,我已经合了代码,辛苦有可能的话再试一下

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants