caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Troubles with marshaled/unmarshaled exception
@ 2003-12-04 15:46 Artem Prisyznuk
  2003-12-04 18:11 ` Alex Baretta
  2003-12-05  0:14 ` Jason Hickey
  0 siblings, 2 replies; 8+ messages in thread
From: Artem Prisyznuk @ 2003-12-04 15:46 UTC (permalink / raw)
  To: caml-bugs; +Cc: caml-list

Hello,

I found strange behavior of pattern matching of
marshaled/unmarshaled exception.

Next code describe problem:

	let e = Failure "test";;
	let e' = Marshal.from_string (Marshal.to_string e []) 0;;
	let print_fun exc =
	  match exc with
	    Failure _ -> print_endline "Matching OK"
	  | exc -> print_endline "Matching Fail";;

	print_fun e;;
	print_fun e';;
       Printf.printf "e = e' is %b\n" (e=e');;

Output:

	Matching OK
	Matching Fail
       e = e' is true

So second call print missing value.

-- 
Artem Prysyznuk
tema@sit.kiev.ua

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [Caml-list] Troubles with marshaled/unmarshaled exception
  2003-12-04 15:46 [Caml-list] Troubles with marshaled/unmarshaled exception Artem Prisyznuk
@ 2003-12-04 18:11 ` Alex Baretta
  2003-12-04 18:51   ` Eric C. Cooper
  2003-12-04 19:14   ` Artem Prisyznuk
  2003-12-05  0:14 ` Jason Hickey
  1 sibling, 2 replies; 8+ messages in thread
From: Alex Baretta @ 2003-12-04 18:11 UTC (permalink / raw)
  To: Artem Prisyznuk, ocaml

Artem Prisyznuk wrote:
> Hello,
> 
> I found strange behavior of pattern matching of
> marshaled/unmarshaled exception.
> 
> Next code describe problem:
> 
>     let e = Failure "test";;
>     let e' = Marshal.from_string (Marshal.to_string e []) 0;;
>     let print_fun exc =


It seems to work alright on 3.07+2.

Here's my session:


         Objective Caml version 3.07+2


# let e = Failure "test";;
val e : exn = Failure "test"
# let e' : exn = Marshal.from_string (Marshal.to_string e []) 0;;
val e' : exn = Failure "test"
# e = e';;
- : bool = true

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [Caml-list] Troubles with marshaled/unmarshaled exception
  2003-12-04 18:11 ` Alex Baretta
@ 2003-12-04 18:51   ` Eric C. Cooper
  2003-12-04 19:05     ` Alex Baretta
  2003-12-04 19:14   ` Artem Prisyznuk
  1 sibling, 1 reply; 8+ messages in thread
From: Eric C. Cooper @ 2003-12-04 18:51 UTC (permalink / raw)
  To: ocaml

On Thu, Dec 04, 2003 at 07:11:51PM +0100, Alex Baretta wrote:
> Artem Prisyznuk wrote:
> >Hello,
> >
> >I found strange behavior of pattern matching of
> >marshaled/unmarshaled exception.
> >
> >Next code describe problem:
> >
> >    let e = Failure "test";;
> >    let e' = Marshal.from_string (Marshal.to_string e []) 0;;
> >    let print_fun exc =
> 
> 
> It seems to work alright on 3.07+2.
> 
> Here's my session:
> 
> 
>         Objective Caml version 3.07+2
> 
> 
> # let e = Failure "test";;
> val e : exn = Failure "test"
> # let e' : exn = Marshal.from_string (Marshal.to_string e []) 0;;
> val e' : exn = Failure "test"
> # e = e';;
> - : bool = true

But something is indeed broken.  Here's a slight variant of the above:

        Objective Caml version 3.07+2

# let e = Failure "test";;
val e : exn = Failure "test"
# let e' = Failure "test";;
val e' : exn = Failure "test"
# let e'' =  Marshal.from_string (Marshal.to_string e []) 0;;
val e'' : 'a = <poly>
# e'' ^ "good-bye";;
Segmentation fault

-- 
Eric C. Cooper          e c c @ c m u . e d u

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [Caml-list] Troubles with marshaled/unmarshaled exception
  2003-12-04 18:51   ` Eric C. Cooper
@ 2003-12-04 19:05     ` Alex Baretta
  2003-12-04 19:50       ` Eric C. Cooper
  0 siblings, 1 reply; 8+ messages in thread
From: Alex Baretta @ 2003-12-04 19:05 UTC (permalink / raw)
  To: Eric C. Cooper, ocaml

Eric C. Cooper wrote:
> On Thu, Dec 04, 2003 at 07:11:51PM +0100, Alex Baretta wrote:

> But something is indeed broken.  Here's a slight variant of the above:
> 
>         Objective Caml version 3.07+2
> 
> # let e = Failure "test";;
> val e : exn = Failure "test"
> # let e' = Failure "test";;
> val e' : exn = Failure "test"
> # let e'' =  Marshal.from_string (Marshal.to_string e []) 0;;
> val e'' : 'a = <poly>
> # e'' ^ "good-bye";;
> Segmentation fault
> 

There's nothing wrong here. Module Marshal is not type safe. If you use 
it incorrectly you cannot complain if your program segfaults. The value 
represented by e'' actually has type exn, however the compiler cannot 
know this because you are defining it through Marshal.from_xxxx, which 
is not type safe. This allows you to type-check e'' ^ "good-bye" which 
segfaults.

Alex

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [Caml-list] Troubles with marshaled/unmarshaled exception
  2003-12-04 18:11 ` Alex Baretta
  2003-12-04 18:51   ` Eric C. Cooper
@ 2003-12-04 19:14   ` Artem Prisyznuk
  1 sibling, 0 replies; 8+ messages in thread
From: Artem Prisyznuk @ 2003-12-04 19:14 UTC (permalink / raw)
  To: Alex Baretta; +Cc: ocaml

On Thu, 04 Dec 2003 19:11:51 +0100, Alex Baretta <alex@baretta.com> wrote:

> # e = e';;
> - : bool = true

Yes, operator = work ok, but pattern matching no :-(.

-- 
Artem Prysyznuk
tema@sit.kiev.ua

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [Caml-list] Troubles with marshaled/unmarshaled exception
  2003-12-04 19:05     ` Alex Baretta
@ 2003-12-04 19:50       ` Eric C. Cooper
  0 siblings, 0 replies; 8+ messages in thread
From: Eric C. Cooper @ 2003-12-04 19:50 UTC (permalink / raw)
  To: ocaml

On Thu, Dec 04, 2003 at 08:05:17PM +0100, Alex Baretta wrote:
> There's nothing wrong here. Module Marshal is not type safe.

You're right, of course.  I'm used to using Marshal in compiled code
where the type is constrained correctly.

Perhaps the interpreter should complain if it ever binds a toplevel
value of type 'a.  Wouldn't it always be an error (or one waiting to
happen)?

-- 
Eric C. Cooper          e c c @ c m u . e d u

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [Caml-list] Troubles with marshaled/unmarshaled exception
  2003-12-04 15:46 [Caml-list] Troubles with marshaled/unmarshaled exception Artem Prisyznuk
  2003-12-04 18:11 ` Alex Baretta
@ 2003-12-05  0:14 ` Jason Hickey
  2003-12-05  7:31   ` Artem Prisyznuk
  1 sibling, 1 reply; 8+ messages in thread
From: Jason Hickey @ 2003-12-05  0:14 UTC (permalink / raw)
  To: Artem Prisyznuk; +Cc: caml-bugs, caml-list

The short answer is: don't marshal exceptions.

The longer answer is that exception marshaling is nearly as hard as 
function/closure marshaling.  I'm sure the OCaml implementors can give a 
more precise description, but here is my understanding.

First,
    1. A program can have a large number of exceptions.
    2. Exception names are scoped.  The following exceptions
       A.Foo and B.Foo are different.  The same holds for
       compilation units/files that are separately compiled.

       module A = struct exception Foo end
       module B = struct exception Foo end

Just like normal unions, the exceptions are eventually represented as a 
block with a tag and some values, but exception tags are resolved at 
*link* time.

This explains why you can't marshal an exception in one process, send it 
over a channel, unmarshal it in another process, and expect it to make 
any sense at all, because the linker's tag selection is likely to be 
different in the two processes.

Why doesn't it work within the same process?  This is most likely 
because the exception tag is a (pointer to a) normal OCaml value, but 
pattern matching on the exception value uses pointer equality for the 
tag.  When you marshal/unmarshal, you deep-copy the tag, and pointer 
equality no longer works (though normal equality works just fine). 
There are excellent implementation reasons to use pointer equality on 
tags; and faithfulness of marshaling is a low priority.

So, it is likely the marshaler could be changed so that exception 
marshaling would work as you might expect, but it would be hard, and not 
very worthwhile.  Don't expect exception marshaling to work in a 
portable manner...

Jason

Artem Prisyznuk wrote:

> Hello,
> 
> I found strange behavior of pattern matching of
> marshaled/unmarshaled exception.
> 
> Next code describe problem:
> 
>     let e = Failure "test";;
>     let e' = Marshal.from_string (Marshal.to_string e []) 0;;
>     let print_fun exc =
>       match exc with
>         Failure _ -> print_endline "Matching OK"
>       | exc -> print_endline "Matching Fail";;
> 
>     print_fun e;;
>     print_fun e';;
>       Printf.printf "e = e' is %b\n" (e=e');;
> 
> Output:
> 
>     Matching OK
>     Matching Fail
>       e = e' is true
> 
> So second call print missing value.
> 

-- 
Jason Hickey                  http://www.cs.caltech.edu/~jyh
Caltech Computer Science      Tel: 626-395-6568 FAX: 626-792-4257

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [Caml-list] Troubles with marshaled/unmarshaled exception
  2003-12-05  0:14 ` Jason Hickey
@ 2003-12-05  7:31   ` Artem Prisyznuk
  0 siblings, 0 replies; 8+ messages in thread
From: Artem Prisyznuk @ 2003-12-05  7:31 UTC (permalink / raw)
  To: Jason Hickey; +Cc: caml-list

Thanks.

I think what put this info to Ocaml Manual is not bad idea. Because I try
find any info in Manual about this topic has zero result.

On Thu, 04 Dec 2003 16:14:40 -0800, Jason Hickey <jyh@cs.caltech.edu> 
wrote:

> The short answer is: don't marshal exceptions.
>
> The longer answer is that exception marshaling is nearly as hard as 
> function/closure marshaling.  I'm sure the OCaml implementors can give a 
> more precise description, but here is my understanding.
>

-- 
Artem Prysyznuk
tema@sit.kiev.ua

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2003-12-05  7:31 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-12-04 15:46 [Caml-list] Troubles with marshaled/unmarshaled exception Artem Prisyznuk
2003-12-04 18:11 ` Alex Baretta
2003-12-04 18:51   ` Eric C. Cooper
2003-12-04 19:05     ` Alex Baretta
2003-12-04 19:50       ` Eric C. Cooper
2003-12-04 19:14   ` Artem Prisyznuk
2003-12-05  0:14 ` Jason Hickey
2003-12-05  7:31   ` Artem Prisyznuk

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).