Skip to content

Instantly share code, notes, and snippets.

@doujiang24
Created October 29, 2014 08:31
Show Gist options
  • Save doujiang24/13f07db5e52d1f332c62 to your computer and use it in GitHub Desktop.
Save doujiang24/13f07db5e52d1f332c62 to your computer and use it in GitHub Desktop.
ffi openssl rsa Segmentation fault
#0 0x00007fe3cd8d0f5e in lj_alloc_free (msp=0x4051f010, ptr=0x7fe3ccdc2bb0 <RSA_private_decrypt>) at lj_alloc.c:1338
#1 0x00007fe3cd87d09d in gc_sweep (g=g@entry=0x4051f3b8, p=0x40ee28f8, lim=38, lim@entry=40) at lj_gc.c:397
#2 0x00007fe3cd87e11b in gc_onestep (L=L@entry=0x4053e998) at lj_gc.c:627
#3 0x00007fe3cd87eaf0 in lj_gc_fullgc (L=0x4053e998) at lj_gc.c:738
#4 0x00007fe3cd88d565 in lua_gc (L=L@entry=0x4053e998, what=what@entry=2, data=<optimized out>) at lj_api.c:1144
#5 0x00007fe3cd8d28d1 in lj_cf_collectgarbage (L=0x4053e998) at lib_base.c:440
#6 0x00007fe3cd87adf5 in lj_BC_FUNCC () from /usr/local/openresty/luajit/lib/libluajit-5.1.so.2
#7 0x00000000004a90cc in ngx_http_lua_run_thread (L=L@entry=0x4051f378, r=r@entry=0x16d7820, ctx=ctx@entry=0x16d8670, nrets=nrets@entry=0)
at ../ngx_lua-0.9.12/src/ngx_http_lua_util.c:980
#8 0x00000000004ac12e in ngx_http_lua_content_by_chunk (L=L@entry=0x4051f378, r=r@entry=0x16d7820)
at ../ngx_lua-0.9.12/src/ngx_http_lua_contentby.c:103
#9 0x00000000004ac453 in ngx_http_lua_content_handler_file (r=0x16d7820) at ../ngx_lua-0.9.12/src/ngx_http_lua_contentby.c:267
#10 0x00000000004abc8c in ngx_http_lua_content_handler (r=0x16d7820) at ../ngx_lua-0.9.12/src/ngx_http_lua_contentby.c:205
#11 0x00000000004455fe in ngx_http_core_content_phase (r=0x16d7820, ph=<optimized out>) at src/http/ngx_http_core_module.c:1418
#12 0x00000000004401b5 in ngx_http_core_run_phases (r=r@entry=0x16d7820) at src/http/ngx_http_core_module.c:899
#13 0x000000000044029d in ngx_http_handler (r=r@entry=0x16d7820) at src/http/ngx_http_core_module.c:882
#14 0x000000000044a9e6 in ngx_http_process_request (r=0x16d7820) at src/http/ngx_http_request.c:1904
#15 0x000000000044af37 in ngx_http_process_request_headers (rev=rev@entry=0x1721ba0) at src/http/ngx_http_request.c:1335
#16 0x000000000044b260 in ngx_http_process_request_line (rev=0x1721ba0) at src/http/ngx_http_request.c:1013
#17 0x0000000000437005 in ngx_epoll_process_events (cycle=<optimized out>, timer=<optimized out>, flags=<optimized out>)
at src/event/modules/ngx_epoll_module.c:691
#18 0x000000000042f847 in ngx_process_events_and_timers (cycle=cycle@entry=0x168de10) at src/event/ngx_event.c:248
#19 0x0000000000435458 in ngx_worker_process_cycle (cycle=cycle@entry=0x168de10, data=data@entry=0x0) at src/os/unix/ngx_process_cycle.c:822
#20 0x0000000000433eb4 in ngx_spawn_process (cycle=cycle@entry=0x168de10, proc=proc@entry=0x4353b0 <ngx_worker_process_cycle>,
data=data@entry=0x0, name=name@entry=0x4d6c76 "worker process", respawn=respawn@entry=-3) at src/os/unix/ngx_process.c:198
#21 0x0000000000435624 in ngx_start_worker_processes (cycle=cycle@entry=0x168de10, n=1, type=type@entry=-3)
at src/os/unix/ngx_process_cycle.c:368
#22 0x0000000000436028 in ngx_master_process_cycle (cycle=cycle@entry=0x168de10) at src/os/unix/ngx_process_cycle.c:140
#23 0x00000000004189cb in main (argc=<optimized out>, argv=<optimized out>) at src/core/nginx.c:407
local resty_rsa = require "resty.rsa"
local pub, err = resty_rsa:new(RSA_PUBLIC_KEY, true, resty_rsa.PADDING.RSA_NO_PADDING)
if not pub then
ngx.say("new rsa err: ", err)
return
end
local s = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
local encrypted, err = pub:encrypt(s)
if not encrypted then
ngx.say("failed to encrypt: ", err)
return
end
ngx.say("encrypted length: ", #encrypted)
local priv, err = resty_rsa:new(RSA_PRIV_KEY, nil, resty_rsa.PADDING.RSA_NO_PADDING)
if not priv then
ngx.say("new rsa err: ", err)
return
end
local decrypted = priv:decrypt(encrypted)
ngx.say(decrypted == s)
collectgarbage()
--[[
library
https://github.com/doujiang24/lua-resty-string/commit/b03e19fcfdb826de4010e5757d54165d0df240c3
--]]
==943== Invalid write of size 8
==943== at 0x52E43B5: lj_alloc_free (lj_alloc.c:1338)
==943== by 0x528E97C: gc_sweep (lj_gc.c:397)
==943== by 0x528FB6B: gc_onestep (lj_gc.c:627)
==943== by 0x529026F: lj_gc_fullgc (lj_gc.c:738)
==943== by 0x529B744: lua_gc (lj_api.c:1144)
==943== by 0x52E5F40: lj_cf_collectgarbage (lib_base.c:440)
==943== by 0x528C414: lj_BC_FUNCC (in /usr/local/openresty-debug/luajit/lib/libluajit-5.1.so.2.1.0)
==943== by 0x4B8578: ngx_http_lua_run_thread (ngx_http_lua_util.c:980)
==943== by 0x4BB11D: ngx_http_lua_content_by_chunk (ngx_http_lua_contentby.c:103)
==943== by 0x4BB4B0: ngx_http_lua_content_handler_file (ngx_http_lua_contentby.c:267)
==943== by 0x4BB5E1: ngx_http_lua_content_handler (ngx_http_lua_contentby.c:205)
==943== by 0x44A423: ngx_http_core_content_phase (ngx_http_core_module.c:1418)
==943== Address 0xfffffff504047b80 is not stack'd, malloc'd or (recently) free'd
==943==
==943==
==943== Process terminating with default action of signal 11 (SIGSEGV)
==943== Access not within mapped region at address 0xFFFFFFF504047B80
==943== at 0x52E43B5: lj_alloc_free (lj_alloc.c:1338)
==943== by 0x528E97C: gc_sweep (lj_gc.c:397)
==943== by 0x528FB6B: gc_onestep (lj_gc.c:627)
==943== by 0x529026F: lj_gc_fullgc (lj_gc.c:738)
==943== by 0x529B744: lua_gc (lj_api.c:1144)
==943== by 0x52E5F40: lj_cf_collectgarbage (lib_base.c:440)
==943== by 0x528C414: lj_BC_FUNCC (in /usr/local/openresty-debug/luajit/lib/libluajit-5.1.so.2.1.0)
==943== by 0x4B8578: ngx_http_lua_run_thread (ngx_http_lua_util.c:980)
==943== by 0x4BB11D: ngx_http_lua_content_by_chunk (ngx_http_lua_contentby.c:103)
==943== by 0x4BB4B0: ngx_http_lua_content_handler_file (ngx_http_lua_contentby.c:267)
==943== by 0x4BB5E1: ngx_http_lua_content_handler (ngx_http_lua_contentby.c:205)
==943== by 0x44A423: ngx_http_core_content_phase (ngx_http_core_module.c:1418)
==943== If you believe this happened as a result of a stack
==943== overflow in your program's main thread (unlikely but
==943== possible), you can try to increase the size of the
==943== main thread stack using the --main-stacksize= flag.
==943== The main thread stack size used in this run was 10485760.
/opt/valgrind/bin/valgrind -q --tool=memcheck --leak-check=full \
--show-possibly-lost=no \
--suppressions=/root/work/openresty/ngx_openresty-1.7.4.1/build/LuaJIT-2.1-20140805/src/lj.supp \
/usr/local/openresty-debug/nginx/sbin/nginx \
> valgrind.out 2>&1
/opt/valgrind/bin/valgrind --version
valgrind-3.8.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment