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

Surface Matching and x64 : heap corruption #170

Closed
rbregier opened this issue Feb 12, 2015 · 30 comments
Closed

Surface Matching and x64 : heap corruption #170

rbregier opened this issue Feb 12, 2015 · 30 comments

Comments

@rbregier
Copy link

Hi,

In surface_matching\src\ppf_match_3d.cpp a function "murmurHash" is used like the following :
KeyType hashKey=0;
murmurHash(key, 4*sizeof(int), 42, &hashKey);

In case of a 64bit system, "murmurHash" is defined via a macro as "hashMurmurx64" in surface_matching\src\hash_murmur.hpp
However this function takes a void* as a parameter which should at least of length (2*sizeof(unsigned int)). Here &hashKey is given, and hashKey is an unsigned int, thus there is a heap corruption.

A workaround consists in forcing "murmurHash" to be defined as hashMurmurx86 even on 32 bit system, but in this case I don't understand the purpose of specific code for x64 systems.

Regards,
Romain

@ahundt
Copy link
Contributor

ahundt commented Nov 13, 2015

do you have a patch for this?

@bmagyar
Copy link
Contributor

bmagyar commented Nov 14, 2015

Those 2 methods are intentional. If they are mixed up, with one alternative the precision of the surface matching drops dramatically, with the other it takes a lot of time.

@mshabunin
Copy link
Contributor

@rbregier , please reopen this issue if you have reproducing example.

@rbregier
Copy link
Author

Well, the example program crashes with the example dataset during training:
ppf_load_match.exe parasaurolophus_6700.ply rs1_normals.ply
execution

In debug mode, Visual Studio throws an exception :
crash_surface_matching

In ppf_match_3d.cpp :
crash_line

And my investigations lead me to the conclusion of my first message.

When asking to Tolga Birdal (the named author in the source code) about it in February, he answered me the following :
"A new version will come out soon hopefully fixing many of the bugs. However, we have tried it on x64 systems, and verified identical results.
As a workaround, you could use hashMurmurx86 on both systems."

My system settings :
Windows 7 64bits
Compiled with Visual Studio 2013 as an x64 executable

So, yes I can provide the fix proposed in my first message, but the resulting performances were not awesome if I remember correctly and according to @bmagyar this fix is suboptimal.
@bmagyar do you have a better solution?

@mshabunin mshabunin added bug and removed wontfix labels Nov 18, 2015
@mshabunin mshabunin reopened this Nov 18, 2015
@bmagyar
Copy link
Contributor

bmagyar commented Nov 18, 2015

At the time of Tolga's GSoC project, I tested this on all platforms I had available: Ubuntu 32bit, Ubuntu 64bit, OSX 64bit. He was using Windows so it is strange that this didn't come up as well as that no warning about this is reported by buildbot (!).

Frankly, I didn't like that macro magic there in the first place, but at the time that was the best shot. This could be solved by using a hashing library that does the 32/64 switching internally, but I'm also curious to see what Tolga is coming up with.

@bmagyar
Copy link
Contributor

bmagyar commented Nov 18, 2015

@tolgabirdal Do you have any thoughts on this?

@tolgabirdal
Copy link
Contributor

This might be due to modification made later. Let us define KeyType as follows:

#if (defined x86_64 || defined _M_X64)
typedef uint64_t KeyType;
#else
typedef unsigned int KeyType;
#endif

I will be submitting a pull request with that.

@rbregier
Copy link
Author

I can confirm that with this definition it runs :

#if (defined x86_64 || defined _M_X64)
#include <stdint.h>
typedef uint64_t KeyType;
#else
typedef unsigned int KeyType;
#endif

However, I get some warnings regarding casts from KeyType to int, unsigned int and size_t. I don't know the effects of those on the algorithm.

@bmagyar
Copy link
Contributor

bmagyar commented Nov 20, 2015

How about using KeyType in favor of int in those instances? Could you try that please?

@rbregier
Copy link
Author

I think it would require a more consequent code refactoring as a default hash function using "unsigned int" (called hash) is used in several place in the code such as for ICP to build hashtables and it is unclear what should be the key type for thoses, but I don't have time to dive into the code and try to figure out how it works in detail. I guess Tolga could fix this easily in his pull request.

@LaurentBerger
Copy link
Contributor

In this question same problem exist in opencv-3.2-dev. I replace code as suggested by @tolgabirdal and it works. May be a PR is necessary.

@tolgabirdal
Copy link
Contributor

I do have it:
#811
But for now seems to have some unsuccessful builds and should be fixed.

@LaurentBerger
Copy link
Contributor

LaurentBerger commented Jan 22, 2017

@tolgabirdal I have got VS 2013. there is no definition for uint64_t in VS2013 (that's OK in vs2015)
you can try :
#if (defined x86_64 || defined _M_X64)
#if _MSC_VER <= 1800
typedef __int64 int64_t;
typedef unsigned __int64 uint64_t;
#endif
typedef uint64_t KeyType;

#else
typedef unsigned int KeyType;
#endif

you will have some warnings :
1>G:\Lib\opencv_contrib\modules\surface_matching\src\t_hash_int.cpp(122): warning C4244: 'argument' : conversion de 'cv::ppf_match_3d::KeyType' en 'unsigned int', perte possible de données
1>G:\Lib\opencv_contrib\modules\surface_matching\src\t_hash_int.cpp(189): warning C4244: 'argument' : conversion de 'cv::ppf_match_3d::KeyType' en 'unsigned int', perte possible de données
1>G:\Lib\opencv_contrib\modules\surface_matching\src\t_hash_int.cpp(214): warning C4244: 'argument' : conversion de 'cv::ppf_match_3d::KeyType' en 'unsigned int', perte possible de données
1>G:\Lib\opencv_contrib\modules\surface_matching\src\ppf_match_3d.cpp(293): warning C4244: '=' : conversion de 'cv::ppf_match_3d::KeyType' en 'int', perte possible de données

@apassenger
Copy link

I am using VS 2013 (64 bit)I tried this:

#if (defined x86_64 || defined _M_X64)
#if _MSC_VER <= 1800
typedef __int64 int64_t;
typedef unsigned __int64 uint64_t;
#endif
typedef uint64_t KeyType;

#else
typedef unsigned int KeyType;
#endif

Unfortunately this could not my problem (My problem : Stack around the variable 'hashKey' was corrupted.)

@tolgabirdal @LaurentBerger

@wangsen1312
Copy link

wangsen1312 commented Apr 7, 2017

@apassenger Did you solve the problem? I have encountered the same problem? still not solved.
My configuration is VS 2013 windows

@opencvbel
Copy link

I am using VS 2015 (64 bit) on a Windows 10 machine with OpenCv 3.2.0 .
I made the changes suggested by @tolgabirdal in the file t_hash_int.hpp where KeyType was defined,
but same runtime error as before which is 'Run-Time Check Failure # 2 - Stack around the variable 'hashKey' was corrupted'.
Any ideas?

@feliwir
Copy link

feliwir commented May 12, 2017

Any updates on this? I am using the 3.2 version and get this error with VS2013 x64 aswell

@JoyMazumder
Copy link

JoyMazumder commented Oct 20, 2018

I have added that but the still same problem. openCV 4.0.0, VS -2015
I also tried at opencv 3.4.2

@JoyMazumder
Copy link

@opencvbel , @apassenger Did you solve that problem?

@asjgit
Copy link

asjgit commented Dec 20, 2018

this problem happend at debug model
at release model processing right, but wrong result

@middlestone
Copy link

is there any progress on this issue? i meet it using VS2017 and openCV 4.0.0 too.

@pclbel
Copy link

pclbel commented Feb 14, 2019

@JoyMazumder No couldnt find any solution, sorry

@tolgabirdal
Copy link
Contributor

tolgabirdal commented Feb 15, 2019 via email

@YYuan-pcl
Copy link

I can confirm that with this definition it runs :

#if (defined x86_64 || defined _M_X64)
#include <stdint.h>
typedef uint64_t KeyType;
#else
typedef unsigned int KeyType;
#endif

However, I get some warnings regarding casts from KeyType to int, unsigned int and size_t. I don't know the effects of those on the algorithm.

It works!However,the performance of the algrithm (after using the changes above)now is quite poor. We cannot even match the samples given in the opencv well.
Very frustrated。。。

@YYuan-pcl
Copy link

In this question same problem exist in opencv-3.2-dev. I replace code as suggested by @tolgabirdal and it works. May be a PR is necessary.

It works!However,the performance of the algrithm (after using the changes above)now is quite poor.

@YYuan-pcl
Copy link

this problem happend at debug model
at release model processing right, but wrong result

It works!However,the performance of the algrithm (after using the changes above)now is quite poor. do you have ideas

@luck-boy1994
Copy link

luck-boy1994 commented Apr 18, 2020

any one solve this problem?
Run-Time Check Failure #2 - Stack around the variable 'hashKey' was corrupted.

static KeyType hashPPF(const Vec4d& f, const double AngleStep, const double DistanceStep)
{
Vec4i key(
(int)(f[0] / AngleStep),
(int)(f[1] / AngleStep),
(int)(f[2] / AngleStep),
(int)(f[3] / DistanceStep));
KeyType hashKey = 0;

murmurHash(key.val, 4*sizeof(int), 42, &hashKey);
return hashKey;
}
opencv_4.0.0 x64 vs2017

@alalek
Copy link
Member

alalek commented Apr 18, 2020

@luck-boy1994 You using outdated version.

Issue is fixed here: #2347

@luck-boy1994
Copy link

@ luck-boy1994您使用的是过时的版本。

问题已在此处修复:#2347

thank you

@YYuan-pcl
Copy link

YYuan-pcl commented Apr 19, 2020 via email

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

No branches or pull requests