Mantis Bug Tracker

View Issue Details Jump to Notes ] Issue History ] Print ]
IDProjectCategoryView StatusDate SubmittedLast Update
0007533OCamlback end (clambda to assembly)public2017-05-12 22:382017-05-15 10:34
Reporterjmi 
Assigned Toshinwell 
PrioritynormalSeverityminorReproducibilityalways
StatusresolvedResolutionduplicate 
PlatformOSOS Version
Product Version4.04.1 
Target VersionFixed in Version 
Summary0007533: Wrong code generation of e / e' and e mod e' with effectful e
DescriptionConsider the examples

  (int_of_string "") / (let e = let w = false in () in 0)
  (int_of_string "") / (List.hd [0])

At runtime the dividend throws an exception and the divisor evaluates to 0.
Under call-by-value, I expect the dividend to be evaluated before attempting a division. This agrees with the bytecode backend's output:

  Fatal error: exception Failure("int_of_string")

However with the native code backend I get:

  Fatal error: exception Division_by_zero

thereby removing the effect of the dividend.


I believe the problem is in asmcomp/cmmgen.ml, line 410-414,
when the divisor is sufficiently complex to not hit (or get constant folded to hit) the earlier cases:

  | (c1, c2) ->
        bind "divisor" c2 (fun c2 ->
          Cifthenelse(c2,
                      Cop(Cdivi, [c1; c2], dbg),
                      raise_symbol dbg "caml_exn_Division_by_zero"))

Note how c1 never gets evaluated when we branch on the result of c2 and it is 0.



There is a similar problem with 'mod' (replacing '/' with 'mod' in the examples):

  (int_of_string "") mod (let e = let w = false in () in 0)
  (int_of_string "") mod (List.hd [0])

and the code generation case for mod in asmcomp/cmmgen.ml, line 446-450.


This is confirmed and reproducable on version 4.04.1 under Debian and 4.04.0 under OSX.


Note, these two issues are different from MPR#7201 (fixed in GPR#954) which concerned how the native code backend removed a Division_by_zero in the 0 / e and 0 mod e cases.
Steps To Reproduce$ echo '(int_of_string "") / (let e = let w = false in () in 0)' > div.ml
$ ocamlopt -o div.native div.ml
$ ocamlc -o div.byte div.ml
$ ./div.native
Fatal error: exception Division_by_zero
$ ./div.byte
Fatal error: exception Failure("int_of_string")

$ echo '(int_of_string "") mod (let e = let w = false in () in 0)' > mod.ml
$ ocamlopt -o mod.native mod.ml
$ ocamlc -o mod.byte mod.ml
$ ./mod.native
Fatal error: exception Division_by_zero
$ ./mod.byte
Fatal error: exception Failure("int_of_string")
TagsNo tags attached.
Attached Files

- Relationships
related to 0007531resolvedshinwell Delayed effects in partially applied functions 

-  Notes
(0017795)
shinwell (developer)
2017-05-15 09:36

Thanks for the report, I will look at this.
(0017796)
shinwell (developer)
2017-05-15 10:34

Superceded by https://github.com/ocaml/ocaml/pull/1173 [^]

- Issue History
Date Modified Username Field Change
2017-05-12 22:38 jmi New Issue
2017-05-12 22:46 gasche Relationship added related to 0007531
2017-05-15 09:36 shinwell Note Added: 0017795
2017-05-15 09:36 shinwell Assigned To => shinwell
2017-05-15 09:36 shinwell Status new => assigned
2017-05-15 10:34 shinwell Note Added: 0017796
2017-05-15 10:34 shinwell Status assigned => resolved
2017-05-15 10:34 shinwell Resolution open => duplicate


Copyright © 2000 - 2011 MantisBT Group
Powered by Mantis Bugtracker