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

The attached code crashes my application on x86_64 while the code works on 32bit systems #4143

Closed
vicuna opened this issue Oct 27, 2006 · 5 comments
Labels

Comments

@vicuna
Copy link

vicuna commented Oct 27, 2006

Original bug ID: 4143
Reporter: cniehaus
Status: closed (set by @xavierleroy on 2007-02-21T14:25:46Z)
Resolution: not a bug
Priority: normal
Severity: crash
Version: 3.09.2
Category: ~DO NOT USE (was: OCaml general)

Bug description

in my application I am using this code to do some facile/ocaml calculations:

static value * solve_equation_closure = NULL;
if (solve_equation_closure == NULL){
· solve_equation_closure = caml_named_value("solve_equation");
}

return strdup(String_val(callback(*solve_equation_closure,
copy_string(eq)) ));

On my 32bit machine this works, but on x86_64 it crashes with this backtrace

Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 46992874527856 (LWP 27127)]
0x00000000004d8180 in camlPervasives__$5e_135 ()
Current language: auto; currently c
(gdb) where
#0 0x00000000004d8180 in camlPervasives__$5e_135 ()
#1 0x00000000619b6b80 in ?? ()
#2 0x00000000004da828 in camlArray__make_matrix_69 ()
#3 0x000000000063a378 in camlLexing__19 ()
#4 0x0000000000000001 in ?? ()
#5 0x00002abd619b6b38 in ?? ()
#6 0x00000000004e24a4 in camlLexing__from_string_152 ()
#7 0x00000000619b6b80 in ?? ()
#8 0x000000000049c089 in camlCalc__create_equation_94 ()
#9 0x00002abd619b6d90 in ?? ()
#10 0x000000000049c0fb in camlCalc__solve_equation_99 ()
#11 0x00007fff4fc7b7e0 in ?? ()
#12 0x000000000049c0d8 in camlCalc__solve_equation_99 ()
#13 0x00000000619b6b80 in ?? ()
#14 0x00002abd619b6b38 in ?? ()
#15 0x0000000000000010 in ?? ()
#16 0x0000000000000000 in ?? ()

Additional information

You can find the whole sourcecode (both c++ and OCaml) here:

http://websvn.kde.org/trunk/KDE/kdeedu/kalzium/src/solver/

@vicuna
Copy link
Author

vicuna commented Oct 27, 2006

Comment author: @oandrieu

callback(*solve_equation_closure, copy_string(eq))

Don't do this: IIRC the order of evaluation of the two operands is unspecified. solve_equation_closure need to be dereferenced after the call to copy_string().

value v=copy_string(eq);
return strdup(String_val(callback(*solve_equation_closure, v)));

@vicuna
Copy link
Author

vicuna commented Oct 27, 2006

Comment author: cniehaus

I tested it, and it crashes again :(

(I even made 'make clean && make' to be sure)

Perhaps it's necessary/better to apply this patch but it doesn't resolve the
crash

@vicuna
Copy link
Author

vicuna commented Jan 28, 2007

Comment author: cniehaus

This C-code creates the same crash. Much easier to track.

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char ** argv)
{
· caml_startup(argv);

· char * eq = " a CH3(CH2)3COOH + b O2 -> c H2O + d CO2";
· char * eq2= " a CH3(CH2)3COOH + b O2 -> c H2O + d CO";
· char * result = solve_equation(eq);
· char * result2 = solve_equation(eq2);

· printf("solution : %s\n", result);

· printf("solution : %s\n", result2);
· free(result2);

· result2 = solve_equation(eq);
· printf("solution : %s\n", result2);
·
· free(result);
· free(result2);
·
· return 0;
}

@vicuna
Copy link
Author

vicuna commented Jan 28, 2007

Comment author: cniehaus

There is an easy way to reproduce this:

svn co svn://anonsvn.kde.org:/home/kde/trunk/KDE/kdeedu/kalzium/src/solver
make test
./testcprog

This crashes on at least three x64 machines I know, works on three of my x86 machines and on mellums alpha (as reported on IRC).

@vicuna
Copy link
Author

vicuna commented Jan 30, 2007

Comment author: @xavierleroy

Olivier Andrieu's comment about evaluation order is correct. You must fix your code as he suggests.

Moreover, you forgot to #include <caml/alloc.h>, so the compiler believes that copy_string returns an int instead of a long. This isn't the same thing on a 64-bit machine. Actually, gcc tells you just that:

ocamlopt -I +facile -c modwrap.c
modwrap.c: In function 'solve_equation':
modwrap.c:35: warning: implicit declaration of function 'caml_copy_string'

@vicuna vicuna closed this as completed Feb 21, 2007
@vicuna vicuna added the bug label Mar 19, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant