diff --git a/.github/workflows/binary-releases.yml b/.github/workflows/binary-releases.yml index 2d464fd2..0baefc43 100644 --- a/.github/workflows/binary-releases.yml +++ b/.github/workflows/binary-releases.yml @@ -3,9 +3,9 @@ name: binary-releases on: push: branches: [master] + workflow_dispatch: jobs: - create-release: runs-on: ubuntu-latest outputs: @@ -20,19 +20,18 @@ jobs: tag_name: ${{ github.run_number }} release_name: Release ${{ github.run_number }} - macos-build: runs-on: macos-latest needs: create-release steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v2 - - name: Opam modules cache - uses: actions/cache@v1 - env: + - name: Opam modules cache + uses: actions/cache@v1 + env: cache-name: cache-opam-modules - with: + with: path: ~/.opam key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('mlang.opam', 'Makefile') }} restore-keys: | @@ -40,27 +39,27 @@ jobs: ${{ runner.os }}-build- ${{ runner.os }}- - - name: Set up OCaml - uses: ocaml/setup-ocaml@v2 - with: + - name: Set up OCaml + uses: ocaml/setup-ocaml@v2 + with: ocaml-compiler: 4.11.2 - - name: Install dependencies - run: | - brew install gmp mpfr - opam update - make init-without-switch + - name: Install dependencies + run: | + brew install gmp mpfr + opam update + make init-without-switch - - name: Make mlang binary - run: | + - name: Make mlang binary + run: | eval $(opam env) make build - - name: Upload release asset - uses: actions/upload-release-asset@v1 - env: + - name: Upload release asset + uses: actions/upload-release-asset@v1 + env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - with: + with: upload_url: ${{ needs.create-release.outputs.upload_url }} asset_path: ./_build/default/src/main.exe asset_name: mlang-macos-v${{ github.run_number }}.exe @@ -95,19 +94,15 @@ jobs: uses: ocaml/setup-ocaml@v2 with: # Version of the OCaml compiler to initialise - ocaml-compiler: ocaml-variants.4.12.0+options,ocaml-option-flambda,ocaml-option-musl,ocaml-option-static + ocaml-compiler: 4.11.2 - name: Install dependencies run: | sudo apt update sudo apt install m4 perl python3 clang git build-essential lzip libgmp-dev libmpfr-dev - wget https://ftp.gnu.org/gnu/gmp/gmp-6.2.1.tar.lz && tar xaf gmp-6.2.1.tar.lz - ( cd gmp-6.2.1; CC=musl-gcc ./configure --prefix /tmp/gmp-prefix && make && make install ) - wget https://www.mpfr.org/mpfr-4.1.0/mpfr-4.1.0.tar.xz && tar xaf mpfr-4.1.0.tar.xz - ( cd mpfr-4.1.0; CC=musl-gcc ./configure --prefix /tmp/gmp-prefix -with-gmp=/tmp/gmp-prefix && make && make install ) eval $(opam env) opam update - MPFR_PREFIX=/tmp/gmp-prefix GMP_PREFIX=/tmp/gmp-prefix make init-without-switch + make init-without-switch - name: Make mlang binary run: | diff --git a/compare_traces.sh b/compare_traces.sh new file mode 100755 index 00000000..715d9f8c --- /dev/null +++ b/compare_traces.sh @@ -0,0 +1,8 @@ +#! /bin/bash +DGFIP_TARGET_FLAGS=-g,-O,-k4,-t YEAR=2022 make compile_dgfip_c_backend -B +~/fuzzing-calculette/calculette/trunk/2022/AIT \ + -f tests/2022/fuzzing/fuzzer_3469.m_test |& \ + sed -e 's/\x1b\[[0-9;]*m//g' &> aif_trace.txt +NO_BINARY_COMPARE=1 ./examples/dgfip_c/ml_primitif/cal \ + tests/2022/fuzzing/fuzzer_3469.m_test &> mlang_trace.txt +diff aif_trace.txt mlang_trace.txt -u > diff_trace.txt diff --git a/examples/dgfip_c/ml_primitif/c_driver_inline_4/var.c b/examples/dgfip_c/ml_primitif/c_driver_inline_4/var.c index 00f2d26f..a0260eda 100644 --- a/examples/dgfip_c/ml_primitif/c_driver_inline_4/var.c +++ b/examples/dgfip_c/ml_primitif/c_driver_inline_4/var.c @@ -16,9 +16,6 @@ int nb_penalite = NB_PENALITE; int nb_restituee = NB_RESTITUEE; int nb_enchaine = NB_ENCH; -int color = 37; -int typo = 0; - #ifdef FLG_DEBUG int nb_err = NB_ERR; #if NB_DEBUG_C <= 0 @@ -54,15 +51,6 @@ T_discord * une_verif(T_irdata *irdata, struct S_discord *(*proc)(T_irdata *irda #endif /* FLG_MULTITHREAD */ } -int change_couleur(int couleur, int typographie) { - color = couleur; - typo = typographie; - return 0; -} - -int get_couleur(void) { - return color; -} struct S_discord * verif_saisie_cohe_primitive_isf(T_irdata *irdata, int appel) { #ifdef FLG_MULTITHREAD diff --git a/examples/dgfip_c/ml_primitif/c_driver_inline_4/var.h b/examples/dgfip_c/ml_primitif/c_driver_inline_4/var.h index b142ec72..357f3006 100644 --- a/examples/dgfip_c/ml_primitif/c_driver_inline_4/var.h +++ b/examples/dgfip_c/ml_primitif/c_driver_inline_4/var.h @@ -44,9 +44,5 @@ extern int nb_verif; #endif /* FLG_DEBUG || FLG_CONTROLE_IMMEDIAT */ extern T_discord *une_verif _PROTS((T_irdata *irdata, struct S_discord *(*proc)(T_irdata *irdata))); - -extern int change_couleur(int couleur, int typographie); -extern int get_couleur(void); - #endif diff --git a/examples/dgfip_c/ml_primitif/c_driver_with_macro/var.c b/examples/dgfip_c/ml_primitif/c_driver_with_macro/var.c index 00f2d26f..92456703 100644 --- a/examples/dgfip_c/ml_primitif/c_driver_with_macro/var.c +++ b/examples/dgfip_c/ml_primitif/c_driver_with_macro/var.c @@ -16,9 +16,6 @@ int nb_penalite = NB_PENALITE; int nb_restituee = NB_RESTITUEE; int nb_enchaine = NB_ENCH; -int color = 37; -int typo = 0; - #ifdef FLG_DEBUG int nb_err = NB_ERR; #if NB_DEBUG_C <= 0 @@ -54,16 +51,6 @@ T_discord * une_verif(T_irdata *irdata, struct S_discord *(*proc)(T_irdata *irda #endif /* FLG_MULTITHREAD */ } -int change_couleur(int couleur, int typographie) { - color = couleur; - typo = typographie; - return 0; -} - -int get_couleur(void) { - return color; -} - struct S_discord * verif_saisie_cohe_primitive_isf(T_irdata *irdata, int appel) { #ifdef FLG_MULTITHREAD init_erreur(irdata); diff --git a/examples/dgfip_c/ml_primitif/c_driver_with_macro/var.h b/examples/dgfip_c/ml_primitif/c_driver_with_macro/var.h index b142ec72..6337418d 100644 --- a/examples/dgfip_c/ml_primitif/c_driver_with_macro/var.h +++ b/examples/dgfip_c/ml_primitif/c_driver_with_macro/var.h @@ -45,8 +45,5 @@ extern int nb_verif; extern T_discord *une_verif _PROTS((T_irdata *irdata, struct S_discord *(*proc)(T_irdata *irdata))); -extern int change_couleur(int couleur, int typographie); -extern int get_couleur(void); - #endif diff --git a/makefiles/mlang.mk b/makefiles/mlang.mk index 1f3cd259..d38db657 100644 --- a/makefiles/mlang.mk +++ b/makefiles/mlang.mk @@ -48,13 +48,12 @@ dune: FORCE ifeq ($(call is_in,),) $(call make_in,,$@) else - dune build $(DUNE_OPTIONS) + LINKING_MODE=$(LINKING_MODE) dune build $(DUNE_OPTIONS) endif build: FORCE | format dune -# Run only in an opam switch with musl and static options activated -build-static: DUNE_OPTIONS+=--profile=static +build-static: LINKING_MODE=static build-static: FORCE build ################################################## diff --git a/src/dune b/src/dune index 507725d6..0dc5ca56 100644 --- a/src/dune +++ b/src/dune @@ -1,13 +1,18 @@ (env - (static - (ocamlopt_flags - (-ccopt -static -O3))) (dev (flags (:standard -warn-error -A)))) +(rule + (with-stdout-to + linking-flags-mlang.sexp + (run ./gen-linking-flags.sh %{env:LINKING_MODE=} %{ocaml-config:system}))) + (executable (name main) (package mlang) (public_name mlang) + (flags + (:standard + (:include linking-flags-mlang.sexp))) (libraries mlang)) diff --git a/src/gen-linking-flags.sh b/src/gen-linking-flags.sh new file mode 100755 index 00000000..bb7170e5 --- /dev/null +++ b/src/gen-linking-flags.sh @@ -0,0 +1,41 @@ +#!/bin/sh +set -ue + +# Copied on https://ocamlpro.com/fr/blog/2021_09_02_generating_static_and_portable_executables_with_ocaml/ + +LINKING_MODE="$1" +OS="$2" +FLAGS= +CCLIB= + +case "$LINKING_MODE" in + '') + ;; # No extra flags needed + static) + case "$OS" in + linux) + FLAGS="-noautolink" + CCLIB="-Wl,-Bstatic -lgmp_caml -lmpfr -lgmp -lnums \ + -lthreadsnat -lparmap_stubs -lANSITerminal_stubs \ + -Wl,-Bdynamic -lpthread -lunix" + LIBS="" + OCAML_LIBS="camlidl" + for lib in $LIBS; do + CCLIB="$CCLIB $(pkg-config $lib --variable libdir)/$lib.a" + done + for lib in $OCAML_LIBS; do + CCLIB="$CCLIB $(ocamlfind query $lib)/lib$lib.a" + done;; + *) + echo "No known static compilation flags for '$OS'" >&2 + exit 1 + esac;; + *) + echo "Invalid linking mode '$LINKING_MODE'" >&2 + exit 2 +esac + +echo '(' +for f in $FLAGS; do echo " $f"; done +for f in $CCLIB; do echo " -cclib $f"; done +echo ')' diff --git a/src/mlang/backend_compilers/bir_to_dgfip_c.ml b/src/mlang/backend_compilers/bir_to_dgfip_c.ml index 7e82b974..4f3e62f1 100644 --- a/src/mlang/backend_compilers/bir_to_dgfip_c.ml +++ b/src/mlang/backend_compilers/bir_to_dgfip_c.ml @@ -288,8 +288,9 @@ let generate_m_assign (dgfip_flags : Dgfip_options.flags) def def_var (D.format_assign dgfip_flags var_indexes val_var) value val_var; - if dgfip_flags.flg_trace then - let var = Bir.var_to_mir var in + (* If the trace flag is set, we print the value of all non-temp variables *) + let var = Bir.var_to_mir var in + if dgfip_flags.flg_trace && not var.Mir.Variable.is_temp then Format.fprintf oc "@;aff2(\"%s\", irdata, %s);" (Pos.unmark var.Mir.Variable.name) (Dgfip_varid.gen_access_pos_from_start var_indexes var) @@ -631,11 +632,13 @@ let generate_target (dgfip_flags : Dgfip_options.flags) (program : Bir.program) (var_indexes : Dgfip_varid.var_id_map) (oc : Format.formatter) ((f, ret_type) : Bir.function_name * bool) = let { tmp_vars; stmts; is_verif; _ } = Mir.TargetMap.find f program.targets in - Format.fprintf oc "@[%a{@,%a%a%s@]@,}@," + Format.fprintf oc "@[%a{@,%a%s@\n%a%s@\n%s@]@,}@," (generate_target_prototype false is_verif) f generate_var_tmp_decls tmp_vars + (if dgfip_flags.flg_trace then "aff1(\"debut " ^ f ^ "\\n\") ;" else "") (generate_stmts dgfip_flags program var_indexes) stmts + (if dgfip_flags.flg_trace then "aff1(\"fin " ^ f ^ "\\n\") ;" else "") (if ret_type then {| #ifdef FLG_MULTITHREAD diff --git a/src/mlang/backend_compilers/decoupledExpr.ml b/src/mlang/backend_compilers/decoupledExpr.ml index 8b2d669c..7c7d11d7 100644 --- a/src/mlang/backend_compilers/decoupledExpr.ml +++ b/src/mlang/backend_compilers/decoupledExpr.ml @@ -6,7 +6,7 @@ type offset = | None let rec generate_variable (vm : Dgfip_varid.var_id_map) (offset : offset) - ?(def_flag = false) ?(debug_flag = false) (var : Bir.variable) : string = + ?(def_flag = false) ?(trace_flag = false) (var : Bir.variable) : string = let mvar = Bir.var_to_mir var in try match offset with @@ -25,7 +25,9 @@ let rec generate_variable (vm : Dgfip_varid.var_id_map) (offset : offset) if def_flag then Dgfip_varid.gen_access_def vm mvar offset else let access_val = Dgfip_varid.gen_access_val vm mvar offset in - if debug_flag then + (* When the trace flag is present, we print the value of the + non-temporary variable being used *) + if trace_flag && not mvar.Mir.Variable.is_temp then let vn = Pos.unmark mvar.Mir.Variable.name in let pos_tgv = Dgfip_varid.gen_access_pos_from_start vm mvar in Format.asprintf "(aff3(\"%s\",irdata, %s), %s)" vn pos_tgv @@ -429,7 +431,7 @@ let format_expr_var (dgfip_flags : Dgfip_options.flags) | M (var, offset, df) -> let def_flag = df = Def in Format.fprintf fmt "%s" - (generate_variable ~debug_flag:dgfip_flags.flg_trace vm offset ~def_flag + (generate_variable ~trace_flag:dgfip_flags.flg_trace vm offset ~def_flag var) let rec format_dexpr (dgfip_flags : Dgfip_options.flags) @@ -488,7 +490,7 @@ let rec format_dexpr (dgfip_flags : Dgfip_options.flags) | Daccess (var, dflag, de) -> Format.fprintf fmt "(%s[(int)%a])" (generate_variable ~def_flag:(dflag = Def) - ~debug_flag:dgfip_flags.flg_trace vm PassPointer var) + ~trace_flag:dgfip_flags.flg_trace vm PassPointer var) format_dexpr de | Dite (dec, det, dee) -> Format.fprintf fmt "@[(%a ?@ %a@ : %a@])" format_dexpr dec diff --git a/src/mlang/backend_compilers/decoupledExpr.mli b/src/mlang/backend_compilers/decoupledExpr.mli index 8b5100b4..4904bf5e 100644 --- a/src/mlang/backend_compilers/decoupledExpr.mli +++ b/src/mlang/backend_compilers/decoupledExpr.mli @@ -9,7 +9,7 @@ val generate_variable : Dgfip_varid.var_id_map -> offset -> ?def_flag:bool -> - ?debug_flag:bool -> + ?trace_flag:bool -> Bir.variable -> string diff --git a/src/mlang/backend_compilers/dgfip_gen_files.ml b/src/mlang/backend_compilers/dgfip_gen_files.ml index 413d3a1f..d09965b0 100644 --- a/src/mlang/backend_compilers/dgfip_gen_files.ml +++ b/src/mlang/backend_compilers/dgfip_gen_files.ml @@ -1412,9 +1412,17 @@ let gen_conf_h fmt flags vars = let gen_dbg fmt = Format.fprintf fmt - {|#ifdef FLG_TRACE + {|#ifdef FLG_COLORS +int change_couleur (int couleur,int typographie); +int get_couleur ( ); +int get_typo ( ); +#endif + +#ifdef FLG_TRACE extern int niv_trace; +extern void aff1(char *nom); + extern void aff_val(const char *nom, const T_irdata *irdata, int indice, int niv, const char *chaine, int is_tab, int expr, int maxi); #define aff2(nom, irdata, indice) aff_val(nom, irdata, indice, 2, "<-", 0, 0, 1) @@ -1679,10 +1687,11 @@ let gen_mlang_h fmt cprog flags vars stats_varinfos rules verifs chainings pr "\n"; gen_annee fmt flags; pr "\n"; - gen_dbg fmt; - pr "\n"; gen_const fmt; pr "\n"; + (* The debug functions need T_irdata to be defined so we put them after *) + gen_dbg fmt; + pr "\n"; gen_decl_varinfos fmt stats_varinfos; pr "\n"; gen_lib fmt flags vars rules verifs chainings errors; @@ -1697,6 +1706,28 @@ let gen_mlang_c fmt = #include "mlang.h" +#ifdef FLG_COLORS +int color = 37; +int typo = 0; + +int change_couleur (int couleur,int typographie) +{ + color = couleur; + typo = typographie; + return 0 ; +} + +int get_couleur ( ) +{ + return color ; +} + +int get_typo ( ) +{ + return typo ; +} +#endif + double floor_g(double a) { if (fabs(a) <= (double)LONG_MAX) { return floor(a); @@ -1898,28 +1929,38 @@ int niv_trace = 3; #define TRACE_FILE stderr #endif /* FLG_API */ +void aff1(nom) +char *nom ; +{ +#ifdef FLG_COLORS +if (niv_trace >= 1) fprintf(stderr, "\033[%d;%dm%s\033[0m", color, typo, nom) ; +#else +if (niv_trace >= 1) fprintf(stderr, "%s \n", nom) ; +#endif +} + void aff_val(const char *nom, const T_irdata *irdata, int indice, int niv, const char *chaine, int is_tab, int expr, int maxi) { double valeur; int def; if (expr < 0) { if (niv_trace >= niv) { -#ifdef FLG_COLOR +#ifdef FLG_COLORS fprintf(TRACE_FILE, "\033[%d;%dm%s[%d] %s 0\033[0m\n", color, typo, nom, expr, chaine); #else fprintf(TRACE_FILE, "%s[%d] %s 0m\n", nom, expr, chaine); -#endif /* FLG_COLOR */ +#endif /* FLG_COLORS */ } return; } else if (expr >= maxi) { -#ifdef FLG_COLOR +#ifdef FLG_COLORS fprintf(TRACE_FILE, "\033[%d;%dmerreur: indice (%d) superieur au maximum (%d)\033[0m\n", color, typo, expr, maxi); #else fprintf(TRACE_FILE, "erreur: indice (%d) superieur au maximum (%d)\n", expr, maxi); -#endif /* FLG_COLOR */ +#endif /* FLG_COLORS */ expr = 0; } #ifdef FLG_COMPACT @@ -1944,66 +1985,66 @@ void aff_val(const char *nom, const T_irdata *irdata, int indice, int niv, const if (is_tab) { if (def == 0) { if (valeur != 0) { -#ifdef FLG_COLOR +#ifdef FLG_COLORS fprintf(TRACE_FILE, "\033[%d;%dm%s[%d] : erreur undef = %lf\033[0m\n", color, typo, nom, expr, valeur); #else fprintf(TRACE_FILE, "%s[%d] : erreur undef = %lf\n", nom, expr, valeur); -#endif /* FLG_COLOR */ +#endif /* FLG_COLORS */ } else if (niv_trace >= niv) { -#ifdef FLG_COLOR +#ifdef FLG_COLORS fprintf(TRACE_FILE, "\033[%d;%dm%s[%d] %s undef\033[0m\n", color, typo, nom, expr, chaine); #else fprintf(TRACE_FILE, "%s[%d] %s undef\n", nom, expr, chaine); -#endif /* FLG_COLOR */ +#endif /* FLG_COLORS */ } } else if (def != 1) { -#ifdef FLG_COLOR +#ifdef FLG_COLORS fprintf(TRACE_FILE, "\033[%d;%dm%s[%d] : erreur flag def = %d\033[0m\n", color, typo, nom, expr, def); #else fprintf(TRACE_FILE, "%s[%d] : erreur flag def = %d\n", nom, expr, def); -#endif /* FLG_COLOR */ +#endif /* FLG_COLORS */ } else if (niv_trace >= niv) { -#ifdef FLG_COLOR +#ifdef FLG_COLORS fprintf(TRACE_FILE, "\033[%d;%dm%s[%d] %s %lf\033[0m\n", color, typo, nom, expr, chaine, valeur); #else fprintf(TRACE_FILE, "%s[%d] %s %lf\n", nom, expr, chaine, valeur); -#endif /* FLG_COLOR */ +#endif /* FLG_COLORS */ } } else { if (def == 0) { if (valeur != 0) { -#ifdef FLG_COLOR +#ifdef FLG_COLORS fprintf(TRACE_FILE, "\033[%d;%dm%s : erreur undef = %lf\033[0m\n", color, typo, nom, valeur); #else fprintf(TRACE_FILE, "%s : erreur undef = %lf\n", nom, valeur); -#endif /* FLG_COLOR */ +#endif /* FLG_COLORS */ } else if (niv_trace >= niv) { -#ifdef FLG_COLOR +#ifdef FLG_COLORS fprintf(TRACE_FILE, "\033[%d;%dm%s %s undef\033[0m\n", color, typo, nom, chaine); #else fprintf(TRACE_FILE, "%s %s undef\n", nom, chaine); -#endif /* FLG_COLOR */ +#endif /* FLG_COLORS */ } } else if (def != 1) { -#ifdef FLG_COLOR +#ifdef FLG_COLORS fprintf(TRACE_FILE, "\033[%d;%dm%s : erreur flag def = %d\033[0m\n", color, typo, nom, def); #else fprintf(TRACE_FILE, "%s : erreur flag def = %d\n", nom, def); -#endif /* FLG_COLOR */ +#endif /* FLG_COLORS */ } else if (niv_trace >= niv) { -#ifdef FLG_COLOR +#ifdef FLG_COLORS fprintf(TRACE_FILE, "\033[%d;%dm%s %s %lf\033[0m\n", color, typo, nom, chaine, valeur); #else fprintf(TRACE_FILE, "%s %s %lf\n", nom, chaine, valeur); -#endif /* FLG_COLOR */ +#endif /* FLG_COLORS */ } } } diff --git a/src/mlang/dune b/src/mlang/dune index 2473ec98..abd143a8 100644 --- a/src/mlang/dune +++ b/src/mlang/dune @@ -1,8 +1,3 @@ -(env - (static - (ocamlopt_flags - (-O3 -ccopt -static)))) - (include_subdirs unqualified) (library