Z3's OCaml library throws segmentation fault - ocaml

Consider the following OCaml code snippet that uses Z3 module to interface with the Z3 solver. The code tries to define a new TPair datatype in Z3 with a single constructor T that accepts two integer arguments:
open Z3
open Z3.SMT
open Z3.Expr
open Z3.Symbol
open Z3.Datatype
open Z3.FuncDecl
open Z3.Arithmetic
open Z3.Arithmetic.Integer
open Z3.Quantifier
let _ =
let cfg = [("model", "true"); ("proof", "false")] in
let ctx = (mk_context cfg) in
let sym = Symbol.mk_string ctx in
let s_Int = mk_sort ctx in
(* (declare-datatypes () ((TPair (T Int Int) )))*)
let s_T = mk_constructor_s ctx "T" (sym "isT")
[sym "left"; sym "right"]
[Some s_Int; Some s_Int] [0; 0] in
let s_TPair = mk_sort_s ctx "TPair" [s_T] in
let _::s_content::_ = Constructor.get_accessor_decls s_T in
let s_isT = Constructor.get_tester_decl s_T in
let solver = Solver.mk_solver ctx None in
begin
Printf.printf "***** CONTEXT ******\n";
print_string ## Solver.to_string solver;
Printf.printf "\n*********************\n"
end
The calls to get_tester_decl and get_accessor_decls both throw segmentation fault. What could be the reason?

No code, written in pure OCaml, can cause a segmentation fault. This is because OCaml is a memory safe language. The problem is somewhere in the plumbing, either in the place where OCaml is bound to the underlying library, on in the z3 library itself.
Only library (or bindings) authors can help you in debugging and fixing this issue. So please, create an issue in the upstream repository.

Related

How to write custom ppx decorator to rescript?

I need to generate a value with a different type from my passed type. This is the first time I write on ocaml-like, and for example, in a familiar me haskell I would use Data.Generics.
How I have understood I need to use decorator and ppx. I wrote simple example
let recordHandler = (loc: Location.t, _recFlag: rec_flag, _t: type_declaration, fields: list(label_declaration)) => {
let (module Builder) = Ast_builder.make(loc);
let test = [%str
let schema: Schema = { name: "", _type: String, properties: [] }
]
let moduleExpr = Builder.pmod_structure(test);
[%str
module S = [%m moduleExpr]
]
}
let str_gen = (~loc, ~path as _, (_rec: rec_flag, t: list(type_declaration))) => {
let t = List.hd(t)
switch t.ptype_kind {
| Ptype_record(fields) => recordHandler(loc, _rec, t, fields);
| _ => Location.raise_errorf(~loc, "schema is used only for records.");
};
};
let name = "my_schema";
let () = {
let str_type_decl = Deriving.Generator.make_noarg(str_gen);
Deriving.add(name, ~str_type_decl) |> Deriving.ignore;
};
And
open Ppxlib;
let _ = Driver.run_as_ppx_rewriter()
But in using in rescript code
module User = {
#deriving(my_schema)
type my_typ = {
foo: int,
};
};
I caught:
schema is not supported
. And I made myself sure me to connect it right when I had changed #deriving(my_schema) for #deriving(abcd) and #deriving(sschema).
I got different error
Ppxlib.Deriving: 'abcd' is not a supported type deriving generator.
And my last experiment was to copy past existing library deriving accessors .
ppx_accessor
I copied-pasted it and renamed for accessors_2. And I got same error such as experiment.
accessors_2 is not supported
Also I haven't found examples "ppx rescript". Can you please help me.
What am I doing wrong (ALL , I know)
I have found answer in the article
Dropping support for custom PPXes such as ppx_deriving (the deriving
attribute is now exclusively interpreted as bs.deriving)

Got error of advancedBy on Swift3 conversion

I'm converting my Swift2.3 code to Swift3, and got an error at the line where I used advancedBy. XCode showing me a replacing code that using offsetBy, but I still don't understand.
func unhideEmaimage(_ imageFile: String) {
let dotLocation = imageFile.characters.index(of: ".")
self.emaImage.texture = SKTexture(imageNamed: imageFile)
if dotLocation != nil {
let filenameInitial = imageFile.startIndex
let filenameLast = dotLocation!.advancedBy(-1)
let filenamePart:String = imageFile[filenameInitial...filenameLast]
}
And, following is the code that XCode showing me how to fix the error. However, I still don't know how to modify.
let filenameLast = "String.CharacterView corresponding to your index".index(dotLocation!, offsetBy: -1)
Please let me know the way to fix this error. Thank you in advance.
You should Write this:
let filenameLast = imageFile.index(dotLocation!,offsetBy: -1)

Pretty-print in js-of-ocaml

I have the following OCaml program
open Js
let lex s = Compiler.Parse_js.lexer_from_file s
let parse s = lex s |> Compiler.Parse_js.parse
let buffer_pp program =
let buf = Buffer.create 10 in
let pp = Compiler.Pretty_print.to_buffer buf in
Compiler.Js_output.program pp program;
Buffer.contents buf |> print_endline
let () =
parse "test.js" |> buffer_pp
and the following JavaScript program
function person(first, last, age, eyecolor) {
this.firstName = first;
this.lastName = last;
this.age = age;
this.eyeColor = eyecolor;
}
person.prototype.name = function() {
return this.firstName + " " + this.lastName;
};
When running the compiled ocaml code, it prints out
function person(first,last,age,eyecolor)
{this.firstName=first;this.lastName=last;this.age=age;this.eyeColor=eyecolor}
person.prototype.name=function(){return this.firstName+" "+this.lastName};
Is there a way to do pretty-printing which displays format better?
You can disable compact mode with
Compiler.Pretty_print.set_compact pp false;
But, AFAIK, it is on by default.
There're also lots of external tools, that prettifies javascript, that you can use, if you're still not satisfied with the result.

Ocamllex Syntactical issue

{ }
rule translate = parse
| "current_directory" { print_string (Sys.getcwd ()) }
| _ as c { print_char c }
| eof { exit 0 }
{
let main () =
let lexbuf = Lexing.from_channel stdin in
while true do
translate lexbuf
done
let _ = Printexc.print main ()
}
Can someone please explain me how the main function works? I have understood the regexp part, and am able to get a sense of the main function, but not the exact meaning.
The main function looks like this:
let main () =
let lexbuf = Lexing.from_channel stdin in
while true do
translate lexbuf
done
It creates a lexbuf using stdin as its source, then calls translate repeatedly using this lexbuf. The type of translate is Lexing.lexbuf -> unit. It expects a lexbuf, looks for one token, and executes the associated rule. You define a rule for eof that causes the program to exit, which terminates the while.
The next line actually runs the main function:
let _ = Printexc.print main ()
What this does is call main, passing it (). If an exception is raised during execution, Printexc.print will print out a description of it. Since no exception is raised in the test, eventually you reach the end of file and the eof rule causes the program to exit.

How to access file given to cilly in my CIL module

I have added a new feature to CIL(C Intermediate Language). I am able to execute my new module using
$cilly --dotestmodule --save-temps -D HAPPY_MOOD -o test test.c
Now, in my testmodule, I want to call Cfg.computeFileCFG for test.c file. But I don't know how to access test.c file in my module.
I tried using Cil.file. but it says "Unbound value Cil.file".
my code:
open Pretty
open Cfg
open Cil
module RD = Reachingdefs
let () = Cfg.computeFileCFG Cil.file
let rec fact n = if n < 2 then 1 else n * fact(n-1)
let doIt n = fact n
let feature : featureDescr =
{ fd_name = "testmodule";
fd_enabled = ref false;
fd_description = "simple test 1240";
fd_extraopt = [];
fd_doit = (function (f: file) -> ignore (doIt 10));
fd_post_check = true;
}
please tell me how to compute the Cfg for test.c file.
I am not a CIL expert, but here are a few remarks:
the CIL online documentation states that Cil.file is an Ocaml type. Passing a type as an argument to a function is probably not what you want to do here;
it seems like the fd_doit function in your feature descriptor takes the file you are looking to process as its argument f;
according to the Cilly manual, the type of f is Cil.file. Conveniently, this seems to be the type of the argument required by the function computeFileCFG.
Hopefully you can take it from here. Good luck!