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

STATUS_ACCESS_VIOLATION or STATUS_STACK_BUFFER_OVERRUN when free_waifu2x() called #4

Open
MolotovCherry opened this issue Jul 28, 2023 · 2 comments

Comments

@MolotovCherry
Copy link
Contributor

MolotovCherry commented Jul 28, 2023

When rust drop runs to free waifu, calling free_waifu2x(), a STATUS_ACCESS_VIOLATION or STATUS_STACK_BUFFER_OVERRUN may occur. After taking a look at this ncnn issue it appears to be the reason why.

In the generated code, we can see that ncnn::Net::~Net() is clearly being run after ncnn::destroy_gpu_instance(), thus causing this issue (of which the link before says that should not happen).

7FF6C1929CFC: E8 6F 82 01 00             callq  0x7ff6c1941f70  ; void ncnn::VulkanDevice::reclaim_blob_allocator(class ncnn::VkAllocator *) const
7FF6C1929D01: 48 8B 53 38                movq   0x38(%rbx), %rdx
7FF6C1929D05: 48 8B 4B 78                movq   0x78(%rbx), %rcx
7FF6C1929D09: E8 5E 84 01 00             callq  0x7ff6c194216c  ; void ncnn::VulkanDevice::reclaim_staging_allocator(class ncnn::VkAllocator *) const
7FF6C1929D0E: E8 31 6D 01 00             callq  0x7ff6c1940a44  ; void ncnn::destroy_gpu_instance(void)
7FF6C1929D13: 48 8D 4B 08                leaq   0x8(%rbx), %rcx
7FF6C1929D17: E8 08 8A 02 00             callq  0x7ff6c1952724  ; ncnn::Net::~Net(void)
7FF6C1929D1C: BA A0 00 00 00             movl   $0xa0, %edx

After removing ncnn::destroy_gpu_instance(); from ~waifu2x() and placing it at the bottom of free_waifu2x() in order to guarantee the destructor order, I have verified the crash is no longer present (and I have not seen a single crash on exit after this patch). As to why this crash didn't occur in simpler code, I have no clue. But I am using this with a complex program with threading and async, and that seems to have triggered the crashes.

I don't know c++, so I do not know how to edit the code in order to guarantee the destructor order (other than placing ncnn::destroy_gpu_instance() in free_waifu2x()), so I can't offer a PR for this, but I think it should probably be easy to solve?

It might also be worth noting that calling the instance destructor here also appears to have the same problem

		this->gpu_count = ncnn::get_gpu_count();
		if (gpuid < 0 || gpuid >= gpu_count)
		{
			fprintf(stderr, "invalid gpu device");
			ncnn::destroy_gpu_instance();
			return;
		}
@darkskygit
Copy link
Owner

i will check this issue tonight 🤔
previously, when I loaded more than 3 models within a process, there would be strange crashes upon program exit
however, the cause was not found before, i suspect that this issue is related to the same problem.

@MolotovCherry
Copy link
Contributor Author

MolotovCherry commented Aug 1, 2023

It might also be worth noting that you can also cause a crash in safe code by opening multiple instances

Of course, I figure that the way resources are used, only 1 instance should ever be allowed (since I believe the constructor opens up some resources that can't be opened multiple times, right?), but regardless, the fact remains it's still possible. (Though being able to use multiple instances might be a very nice thing!)

use waifu2x::Waifu2x;

fn main() {
    let waifu = Waifu2x::new(0, 3, 2, 128, true);
    let waifu2 = Waifu2x::new(0, 3, 2, 128, true);
}
     Running `R:\Temp\rust\tests-3375732154\debug\tests.exe`
error: process didn't exit successfully: `R:\Temp\rust\tests-3375732154\debug\tests.exe` (exit code: 0xc0000409, STATUS_STACK_BUFFER_OVERRUN)

MolotovCherry added a commit to MolotovCherry/waifu2x.rs that referenced this issue Dec 10, 2023
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