<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<style type="text/css" style="display:none;"> P {margin-top:0;margin-bottom:0;} </style>
</head>
<body dir="ltr">
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
Hi, <br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
I'm apologizing for posting an issue without checking all the small details beforehand.
<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
I recompiled again and saw a warning telling that SCIPgetBestSol is implicitly declared, and as a result the pointer is assigned without casting.
<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
I added the header file in which SCIPgetBestSol is declared, recompiled (now without any warnings) and run the program again.
<br>
It seems that the issue is resolved. <br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
So, I'm closing the issue, and apologizing again for spamming. <br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
Avrech<br>
</div>
<div id="appendonsend"></div>
<hr style="display:inline-block;width:98%" tabindex="-1">
<div id="divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" style="font-size:11pt" color="#000000"><b>From:</b> Avrech Ben-David<br>
<b>Sent:</b> Sunday, May 31, 2020 11:20 AM<br>
<b>To:</b> scip@zib.de <scip@zib.de><br>
<b>Subject:</b> Segmentation fault caused by 64-bit address returned from SCIPgetBestSol</font>
<div> </div>
</div>
<style type="text/css" style="display:none">
<!--
p
        {margin-top:0;
        margin-bottom:0}
-->
</style>
<div dir="ltr">
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
Hello,</div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<br>
</div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
I'm using SCIP and PySCIPOpt for learning better cut selection algorithm.</div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
I added some functionality to SCIP source code, to help me calculate the directed cutoff distance as it is done in SCIPselectCuts()</div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
My function is:</div>
<span><br>
</span>
<blockquote style="border-color:rgb(200,200,200); border-left:3px solid rgb(200,200,200); padding-left:1ex; margin-left:0.8ex; color:rgb(102,102,102)">
<span>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<span style="font-family:calibri,arial,helvetica,sans-serif; font-size:12pt; color:rgb(102,102,102); background-color:rgba(0,0,0,0)">SCIP_Real SCIProwGetDirCutoffDistance(</span><span><br>
</span>
<div><span style="font-family:calibri,arial,helvetica,sans-serif; font-size:12pt; color:rgb(102,102,102); background-color:rgba(0,0,0,0)">   SCIP*                 scip,               /**< SCIP data structure */</span><br>
</div>
<div><span style="font-family:calibri,arial,helvetica,sans-serif; font-size:12pt; color:rgb(102,102,102); background-color:rgba(0,0,0,0)">   SCIP_ROW*             row                 /**< LP row */</span><br>
</div>
<div><span style="font-family:calibri,arial,helvetica,sans-serif; font-size:12pt; color:rgb(102,102,102); background-color:rgba(0,0,0,0)">   )</span><br>
</div>
<div><span style="font-family:calibri,arial,helvetica,sans-serif; font-size:12pt; color:rgb(102,102,102); background-color:rgba(0,0,0,0)">{</span><br>
</div>
<div><span style="font-family:calibri,arial,helvetica,sans-serif; font-size:12pt; color:rgb(102,102,102); background-color:rgba(0,0,0,0)">    /* get the best current solution as in SCIPselectCuts */</span><br>
</div>
<div><span style="font-family:calibri,arial,helvetica,sans-serif; font-size:12pt; color:rgb(102,102,102); background-color:rgba(0,0,0,0)">    SCIP_SOL* sol = SCIPgetBestSol(scip);</span><br>
</div>
<div><span style="font-family:calibri,arial,helvetica,sans-serif; font-size:12pt; color:rgb(102,102,102); background-color:rgba(0,0,0,0)">    if( sol != NULL )</span><br>
</div>
<div><span style="font-family:calibri,arial,helvetica,sans-serif; font-size:12pt; color:rgb(102,102,102); background-color:rgba(0,0,0,0)">    {</span><br>
</div>
<div><span style="font-family:calibri,arial,helvetica,sans-serif; font-size:12pt; color:rgb(102,102,102); background-color:rgba(0,0,0,0)">        return SCIProwGetLPSolCutoffDistance(row, scip->set, scip->stat, sol, scip->lp);</span><br>
</div>
<div><span style="font-family:calibri,arial,helvetica,sans-serif; font-size:12pt; color:rgb(102,102,102); background-color:rgba(0,0,0,0)">    }</span><br>
</div>
<div><span style="font-family:calibri,arial,helvetica,sans-serif; font-size:12pt; color:rgb(102,102,102); background-color:rgba(0,0,0,0)">    else</span><br>
</div>
<div><span style="font-family:calibri,arial,helvetica,sans-serif; font-size:12pt; color:rgb(102,102,102); background-color:rgba(0,0,0,0)">    {</span><br>
</div>
<div><span style="font-family:calibri,arial,helvetica,sans-serif; font-size:12pt; color:rgb(102,102,102); background-color:rgba(0,0,0,0)">        return 0.0;</span><br>
</div>
<div><span style="font-family:calibri,arial,helvetica,sans-serif; font-size:12pt; color:rgb(102,102,102); background-color:rgba(0,0,0,0)">    }</span><br>
</div>
</div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<span style="font-family:calibri,arial,helvetica,sans-serif; font-size:12pt; color:rgb(102,102,102); background-color:rgba(0,0,0,0)">}</span></div>
</span></blockquote>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
I implemented this function in `lp.c`, declared it with `SCIP_EXPORT` in `pub_lp.h`, and correspondingly added the declaration to PySCIPOpt `scip.pxd`.<br>
</div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
In runtime, the function is called from within a separator.sepaexeclp(), which is written in Python.
<br>
</div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<span>My program runs, and at some point, after solving many instances without any problem,<br>
</span></div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<span>I get a segmentation fault:</span></div>
<blockquote style="border-color:rgb(200,200,200); border-left:3px solid rgb(200,200,200); padding-left:1ex; margin-left:0.8ex; color:rgb(102,102,102)">
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<span><span style="font-family:calibri,arial,helvetica,sans-serif; font-size:12pt; color:rgb(102,102,102); background-color:rgba(0,0,0,0)">Thread 1 "python" received signal SIGSEGV, Segmentation fault.</span><span><br>
</span>
<div><span style="font-family:calibri,arial,helvetica,sans-serif; font-size:12pt; color:rgb(102,102,102); background-color:rgba(0,0,0,0)">0x00007fffd7b3e3c4 in SCIPsolGetVal (sol=0xffffffff8043b850, set=0x7e470390, stat=0x7fc95db0, var=0x60eb2b78) at /home/avrech/scipoptsuite-6.0.2-avrech/scip/src/scip/sol.c:1314</span><br>
</div>
<div><span style="font-family:calibri,arial,helvetica,sans-serif; font-size:12pt; color:rgb(102,102,102); background-color:rgba(0,0,0,0)">1314   assert(sol->solorigin == SCIP_SOLORIGIN_ORIGINAL</span><br>
</div>
<div><span style="font-family:calibri,arial,helvetica,sans-serif; font-size:12pt; color:rgb(102,102,102); background-color:rgba(0,0,0,0)">1: sol = (SCIP_SOL *) 0xffffffff8043b850</span><br>
</div>
<span></span><span style="font-family:calibri,arial,helvetica,sans-serif; font-size:12pt; color:rgb(102,102,102); background-color:rgba(0,0,0,0)">2: *sol = <error: Cannot access memory at address 0xffffffff8043b850></span><span><br>
</span></span></div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<span><span></span></span><span style="font-family:calibri,arial,helvetica,sans-serif; font-size:12pt; color:rgb(102,102,102); background-color:rgba(0,0,0,0)">...</span></div>
</blockquote>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
The code crashes while trying to access `sol->solorigin`.<span style="font-family:calibri,arial,helvetica,sans-serif; font-size:12pt; color:rgb(102,102,102); background-color:rgba(0,0,0,0)"><br>
</span></div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<span>Printing the backtrace:<br>
<blockquote style="border-color:rgb(200,200,200); border-left:3px solid rgb(200,200,200); padding-left:1ex; margin-left:0.8ex; color:rgb(102,102,102)">
<div><span>(gdb) bt<br>
</span>
<div>#0  0x00007fffd7b3e3c4 in SCIPsolGetVal (sol=0xffffffff8043b850, set=0x7e470390, stat=0x7fc95db0, var=0x60eb2b78) at /home/avrech/scipoptsuite-6.0.2-avrech/scip/src/scip/sol.c:1314<br>
</div>
<div>#1  0x00007fffd78b5b73 in SCIProwGetLPSolCutoffDistance (row=0x7c3b9ff0, set=0x7e470390, stat=0x7fc95db0, sol=0xffffffff8043b850, lp=0x790ce040)<br>
</div>
<div>    at /home/avrech/scipoptsuite-6.0.2-avrech/scip/src/scip/lp.c:6730<br>
</div>
<div>#2  0x00007fffd78b5eba in SCIProwGetDirCutoffDistance (scip=0x7eecb5c0, row=0x7c3b9ff0) at /home/avrech/scipoptsuite-6.0.2-avrech/scip/src/scip/lp.c:6772<br>
</div>
<div>#3  0x00007fffd84c8229 in __pyx_pf_9pyscipopt_4scip_5Model_530getState (__pyx_v_prev_state=<optimized out>, __pyx_v_state_format=0x7ffff5aa45e0, __pyx_v_query=<optimized out>,
<br>
</div>
<div>    __pyx_v_get_available_cuts=0x9d3580 <_Py_TrueStruct>, __pyx_v_self=<optimized out>) at src/pyscipopt/scip.c:138080<br>
</div>
</div>
</blockquote>
<div>I see that `sol` is unexpectedly 64-bit address.</div>
<div>This pointer was generated by calling ```<span>SCIP_SOL* sol = SCIPgetBestSol(scip);</span>``` above.
<br>
</div>
<div>I trace back to the frame where it was generated, and finds that it is really 64-bit.</div>
<blockquote style="border-color:rgb(200,200,200); border-left:3px solid rgb(200,200,200); padding-left:1ex; margin-left:0.8ex; color:rgb(102,102,102)">
<div><span>(gdb) frame 2<br>
</span>
<div>#2  0x00007fffd78b5eba in SCIProwGetDirCutoffDistance (scip=0x7eecb5c0, row=0x7c3b9ff0) at /home/avrech/scipoptsuite-6.0.2-avrech/scip/src/scip/lp.c:6772<br>
</div>
<div>warning: Source file is more recent than executable.<br>
</div>
<div>6772   case 'd':<br>
</div>
<div>(gdb) info locals<br>
</div>
</div>
<div><span>sol = 0xffffffff8043b850</span></div>
<div><span><span>(gdb) display sol<br>
</span>
<div>81: sol = (SCIP_SOL *) 0xffffffff<span><span>8043b850</span></span><br>
</div>
<div>(gdb) display *sol<br>
</div>
<span>82: *sol = <error: Cannot access memory at address 0xffffffff<span><span>8043b850</span></span>></span><br>
</span></div>
</blockquote>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
Looking into `<span><span></span></span><span><span><span>SCIPgetBestSol(scip)</span></span>`, I see that it returns `scip->primal->sols[0]`, which is stored correctly as a 32-bit address:<br>
<blockquote style="border-color:rgb(200,200,200); border-left:3px solid rgb(200,200,200); padding-left:1ex; margin-left:0.8ex; color:rgb(102,102,102)">
<div><span>(gdb) display scip->primal->sols[0]<br>
</span>
<div>100: scip->primal->sols[0] = (SCIP_SOL *) 0x8043b850</div>
</div>
</blockquote>
<div>Moreover, if I override `sol` manually using `<span><span><span></span></span><span><span><span>SCIPgetBestSol(scip)</span></span></span></span>` it gets its correct 32-bit value:</div>
<blockquote style="border-color:rgb(200,200,200); border-left:3px solid rgb(200,200,200); padding-left:1ex; margin-left:0.8ex; color:rgb(102,102,102)">
<div><span>(gdb) set var sol = SCIPgetBestSol(scip)<br>
</span>
<div>(gdb) display sol<br>
</div>
<div>101: sol = (SCIP_SOL *) 0x8043b850<br>
</div>
<div>(gdb) display *sol<br>
</div>
<div>102: *sol = {obj = -14.386630020491893, time = 0.031049999999999998, ..., solorigin = SCIP_SOLORIGIN_ZERO, hasinfval = 0}<br>
</div>
<div>(gdb) display sol->solorigin<br>
</div>
<div>103: sol->solorigin = SCIP_SOLORIGIN_ZERO<br>
</div>
</div>
</blockquote>
<div><span></span>Now that `sol` is a 32-bit address, it is accessible, and everything seems to work fine.
<br>
<br>
I don't understand how this faulty casting into 64-bit address happens. <br>
</div>
<div><span><span>It seems to happen sporadically at any step of the solving process.
<br>
</span></span></div>
<div><span><span>I reproduced it many times, it can happen after solving hundreds of instances, either in the first separation round, or after many LP rounds in which</span></span></div>
<div><span><span>it doesn't make any problems. <br>
</span></span></div>
<div><br>
</div>
<div>What could be the reason for this weird segfault?</div>
<div>Have I done something wrong in my code?<br>
</div>
<div>Is there an alternative way to compute the directed cutoff distance of a LP row using PySCIPOpt?</div>
<div><br>
</div>
<div><span><span>I didn't look in the compilation output for warnings when I compiled it the last time.</span></span></div>
<div>If needed, I will compile again and print out the warnings if any exists. <br>
</div>
<div><br>
</div>
<div>More technical details:</div>
<div>I'm using <br>
</div>
<div>scipoptsuite-6.0.2 (with some extensions as shown)</div>
<div>PySCIPOpt-2.2.3 (also with some extensions)</div>
<div><span>gcc (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0<br>
</span>Python 3.6.9 (virtualenv)<br>
</div>
<div>pip 20.1.1</div>
<div>Cython 0.29.18</div>
<div><br>
</div>
<div><br>
</div>
<div>Thank you for helping!</div>
<div>Avrech<br>
</div>
<div><br>
</div>
<div><br>
</div>
<div><span></span><br>
</div>
</span></div>
</span></div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<span><br>
</span></div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<span><br>
</span></div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<span><br>
</span></div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<span><br>
</span></div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<span><br>
</span></div>
</div>
</body>
</html>