herumi / mcl

a portable and fast pairing-based cryptography library
BSD 3-Clause "New" or "Revised" License
472 stars 155 forks source link

SIGSEV in mclBn_pairing when using Valgrind #113

Closed jdv-ibm closed 3 years ago

jdv-ibm commented 3 years ago

Hi.

There seems to be a weird memory bug somewhere, that shows up as a Segmentation Fault when using Valgrind to profile programs that use the mclBn_pairing through the C API. I have been able to track it down to this call to sqrPre from dblLineWithoutP < dblLine < millerLoop < mclBn_pairing.

The specific error I get is:

==17709== Process terminating with default action of signal 11 (SIGSEGV)
==17709==  Bad permissions for mapped region at address 0x1FFEFFEAC0
==17709==    at 0x1FFEFFEAC0: ???

The code below is a minimal working sample. If run normally, it returns without error. If run under Valgrind, it returns the previous SIGSEV.

#include <stdio.h>
#include <string.h>

#include "mcl/bn_c384_256.h"
#include "mcl/bn.h"

#define GEN_P "1 3685416753713387016781088315183077757961620795782546409894578378688607592378376318836054947676345821548104185464507 1339506544944476473020471379941921221584933875938349620426543736416511423956333506472724655353366534992391756441569"

#define GEN_Q "1 352701069587466618187139116011060144890029952792775240219908644239793785735715026873347600343865175952761926303160 3059144344244213709971259814753781636986470325476647558659373206291635324768958432433509563104347017837885763365758 1985150602287291935568054521177171638300868978215655730859378665066344726373823718423869104263333984641494340347905 927553665492332455747201965776037880757740193453592970025027978793976877002675564980949289727957565575433344219582"

int main (int argc, char *argv[]) {

  mclBnGT dst;
  mclBnG1 P;
  mclBnG2 Q;

  mclBn_init(MCL_BLS12_381, MCLBN_COMPILED_TIME_VAR);
  mclBnG1_setStr(&P, GEN_P, strlen(GEN_P), 10);
  mclBnG2_setStr(&Q, GEN_Q, strlen(GEN_Q), 10);
  mclBn_pairing(&dst, &P, &Q);  

  return 0;

}
herumi commented 3 years ago

Thank you for the report. I could reproduce the problem. I'll investigate it.

herumi commented 3 years ago

valgrind does not support adcx and adox. amd64 instructions ADCX and ADOX are not implemented in VEX https://bugs.kde.org/show_bug.cgi?id=360415

xbyak/sample% ./test_util64
64bit
vendor intel
 mmx mmx2 cmov sse sse2 sse3 ssse3 sse41 sse42 popcnt aesni rdtscp osxsave(xgetvb) pclmulqdq avx fma avx2 bmi1 bmi2 lzcnt prefetchw enh_rep rdrand adx rdseed smap f16c movbe
popcnt ng 0 13
family=6, model=E, stepping=9, extFamily=0, extModel=9
display:family=6, model=9E
cache level=0 data cache size=32768 cores sharing data cache=1
cache level=1 data cache size=262144 cores sharing data cache=1
cache level=2 data cache size=8388608 cores sharing data cache=1
SmtLevel =1
CoreLevel=1
xbyak/sample% valgrind ./test_util64
==10770== Memcheck, a memory error detector
==10770== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==10770== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==10770== Command: ./test_util64
==10770==
64bit
vendor intel
 mmx mmx2 cmov sse sse2 sse3 ssse3 sse41 sse42 popcnt aesni rdtscp osxsave(xgetvb) pclmulqdq avx fma avx2 bmi1 bmi2 lzcnt enh_rep rdrand f16c movbe
popcnt ng 0 13
family=6, model=C, stepping=3, extFamily=0, extModel=3
display:family=6, model=3C
cache level=0 data cache size=32768 cores sharing data cache=1
cache level=1 data cache size=262144 cores sharing data cache=1
cache level=2 data cache size=8388608 cores sharing data cache=4
SmtLevel =2
CoreLevel=4
herumi commented 3 years ago

I've fixed the bug at https://github.com/herumi/mcl/commit/c41e9e459d98f5ffc74d6c23520dd48c941b869c and add tested it by valgrind. https://github.com/herumi/mcl/runs/2182225198

jdv-ibm commented 3 years ago

Works perfectly. Thanks!