From b67d3e5447f4b2bca3ed92f3db84adb3f79f9b16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Gustavsson?= Date: Wed, 9 Nov 2011 17:27:52 +0100 Subject: BEAM loader: Clean up handling of error reasons There is no reason to have erts_load_module() return integer values, only to have the caller convert the values to atoms. Return the appropriate atom directly from the place where the error is generated instead. Return NIL if the module was successfully loaded. --- erts/emulator/beam/beam_bif_load.c | 42 ++++++++------------------------------ erts/emulator/beam/beam_load.c | 40 ++++++++++++++++++------------------ erts/emulator/beam/beam_load.h | 3 ++- erts/emulator/beam/erl_init.c | 7 ++++--- erts/emulator/beam/global.h | 2 +- 5 files changed, 36 insertions(+), 58 deletions(-) (limited to 'erts/emulator') diff --git a/erts/emulator/beam/beam_bif_load.c b/erts/emulator/beam/beam_bif_load.c index 9ee4bb6878..e57becbe9f 100644 --- a/erts/emulator/beam/beam_bif_load.c +++ b/erts/emulator/beam/beam_bif_load.c @@ -50,7 +50,6 @@ load_module_2(BIF_ALIST_2) { Eterm reason; Eterm* hp; - int i; int sz; byte* code; Eterm res; @@ -69,38 +68,15 @@ load_module_2(BIF_ALIST_2) hp = HAlloc(BIF_P, 3); sz = binary_size(BIF_ARG_2); - if ((i = erts_load_module(BIF_P, 0, - BIF_P->group_leader, &BIF_ARG_1, code, sz)) < 0) { - switch (i) { - case -1: reason = am_badfile; break; - case -2: reason = am_nofile; break; - case -3: reason = am_not_purged; break; - case -4: - reason = am_atom_put("native_code", sizeof("native_code")-1); - break; - case -5: - { - /* - * The module contains an on_load function. The loader - * has loaded the module as usual, except that the - * export entries does not point into the module, so it - * is not possible to call any code in the module. - */ - - ERTS_DECL_AM(on_load); - reason = AM_on_load; - break; - } - default: reason = am_badfile; break; - } + reason = erts_load_module(BIF_P, 0, BIF_P->group_leader, + &BIF_ARG_1, code, sz); + if (reason != NIL) { res = TUPLE2(hp, am_error, reason); - goto done; + } else { + set_default_trace_pattern(BIF_ARG_1); + res = TUPLE2(hp, am_module, BIF_ARG_1); } - set_default_trace_pattern(BIF_ARG_1); - res = TUPLE2(hp, am_module, BIF_ARG_1); - - done: erts_free_aligned_binary_bytes(temp_alloc); erts_smp_thr_progress_unblock(); erts_smp_proc_lock(BIF_P, ERTS_PROC_LOCK_MAIN); @@ -769,7 +745,7 @@ delete_export_references(Eterm module) } -int +Eterm beam_make_current_old(Process *c_p, ErtsProcLocks c_p_locks, Eterm module) { Module* modp = erts_put_module(module); @@ -780,12 +756,12 @@ beam_make_current_old(Process *c_p, ErtsProcLocks c_p_locks, Eterm module) */ if (modp->code != NULL && modp->old_code != NULL) { - return -3; + return am_not_purged; } else if (modp->old_code == NULL) { /* Make the current version old. */ delete_code(c_p, c_p_locks, modp); delete_export_references(module); } - return 0; + return NIL; } static int diff --git a/erts/emulator/beam/beam_load.c b/erts/emulator/beam/beam_load.c index e98210d39c..883689f9d3 100644 --- a/erts/emulator/beam/beam_load.c +++ b/erts/emulator/beam/beam_load.c @@ -494,10 +494,10 @@ typedef struct { } while (0) -static int bin_load(Process *c_p, ErtsProcLocks c_p_locks, +static Eterm bin_load(Process *c_p, ErtsProcLocks c_p_locks, Eterm group_leader, Eterm* modp, byte* bytes, int unloaded_size); static void init_state(LoaderState* stp); -static int insert_new_code(Process *c_p, ErtsProcLocks c_p_locks, +static Eterm insert_new_code(Process *c_p, ErtsProcLocks c_p_locks, Eterm group_leader, Eterm module, BeamInstr* code, Uint size, BeamInstr catches); static int scan_iff_file(LoaderState* stp, Uint* chunk_types, @@ -592,7 +592,7 @@ define_file(LoaderState* stp, char* name, int idx) stp->file_left = stp->chunks[idx].size; } -int +Eterm erts_load_module(Process *c_p, ErtsProcLocks c_p_locks, Eterm group_leader, /* Group leader or NIL if none. */ @@ -604,7 +604,7 @@ erts_load_module(Process *c_p, int size) /* Size of code to load. */ { ErlDrvBinary* bin; - int result; + Eterm result; if (size >= 4 && code[0] == 'F' && code[1] == 'O' && code[2] == 'R' && code[3] == '1') { @@ -617,7 +617,7 @@ erts_load_module(Process *c_p, * The BEAM module is compressed (or possibly invalid/corrupted). */ if ((bin = (ErlDrvBinary *) erts_gzinflate_buffer((char*)code, size)) == NULL) { - return -1; + return am_badfile; } result = bin_load(c_p, c_p_locks, group_leader, modp, (byte*)bin->orig_bytes, bin->orig_size); @@ -638,12 +638,12 @@ extern void check_allocated_block(Uint type, void *blk); #define CHKBLK(TYPE,BLK) /* nothing */ #endif -static int +static Eterm bin_load(Process *c_p, ErtsProcLocks c_p_locks, Eterm group_leader, Eterm* modp, byte* bytes, int unloaded_size) { LoaderState state; - int rval = -1; + Eterm retval = am_badfile; init_state(&state); state.module = *modp; @@ -793,9 +793,9 @@ bin_load(Process *c_p, ErtsProcLocks c_p_locks, */ CHKBLK(ERTS_ALC_T_CODE,state.code); - rval = insert_new_code(c_p, c_p_locks, state.group_leader, state.module, - state.code, state.loaded_size, state.catches); - if (rval < 0) { + retval = insert_new_code(c_p, c_p_locks, state.group_leader, state.module, + state.code, state.loaded_size, state.catches); + if (retval != NIL) { goto load_error; } CHKBLK(ERTS_ALC_T_CODE,state.code); @@ -811,7 +811,6 @@ bin_load(Process *c_p, ErtsProcLocks c_p_locks, debug_dump_code(state.code,state.ci); #endif #endif - rval = 0; state.code = NULL; /* Prevent code from being freed. */ *modp = state.module; @@ -820,7 +819,7 @@ bin_load(Process *c_p, ErtsProcLocks c_p_locks, * indicate that the on_load function must be run. */ if (state.on_load) { - rval = -5; + retval = am_on_load; } load_error: @@ -883,7 +882,7 @@ bin_load(Process *c_p, ErtsProcLocks c_p_locks, erts_free(ERTS_ALC_T_LOADER_TMP, state.fname); } - return rval; + return retval; } @@ -919,21 +918,22 @@ init_state(LoaderState* stp) stp->fname = 0; } -static int +static Eterm insert_new_code(Process *c_p, ErtsProcLocks c_p_locks, - Eterm group_leader, Eterm module, BeamInstr* code, Uint size, BeamInstr catches) + Eterm group_leader, Eterm module, BeamInstr* code, + Uint size, BeamInstr catches) { Module* modp; - int rval; + Eterm retval; int i; - if ((rval = beam_make_current_old(c_p, c_p_locks, module)) < 0) { + if ((retval = beam_make_current_old(c_p, c_p_locks, module)) < 0) { erts_dsprintf_buf_t *dsbufp = erts_create_logger_dsbuf(); erts_dsprintf(dsbufp, "Module %T must be purged before loading\n", module); erts_send_error_to_logger(group_leader, dsbufp); - return rval; + return retval; } /* @@ -966,7 +966,7 @@ insert_new_code(Process *c_p, ErtsProcLocks c_p_locks, modules[i].end = (BeamInstr *) (((byte *)code) + size); num_loaded_modules++; mid_module = &modules[num_loaded_modules/2]; - return 0; + return NIL; } static int @@ -5889,7 +5889,7 @@ erts_make_stub_module(Process* p, Eterm Mod, Eterm Beam, Eterm Info) rval = insert_new_code(p, 0, p->group_leader, Mod, code, code_size, BEAM_CATCHES_NIL); - if (rval < 0) { + if (rval != NIL) { goto error; } diff --git a/erts/emulator/beam/beam_load.h b/erts/emulator/beam/beam_load.h index 9d4a60fed1..2956bd0add 100644 --- a/erts/emulator/beam/beam_load.h +++ b/erts/emulator/beam/beam_load.h @@ -23,7 +23,8 @@ #include "beam_opcodes.h" #include "erl_process.h" -int beam_make_current_old(Process *c_p, ErtsProcLocks c_p_locks, Eterm module); +Eterm beam_make_current_old(Process *c_p, ErtsProcLocks c_p_locks, + Eterm module); typedef struct gen_op_entry { char* name; diff --git a/erts/emulator/beam/erl_init.c b/erts/emulator/beam/erl_init.c index fd15df731a..7ae9f990ad 100644 --- a/erts/emulator/beam/erl_init.c +++ b/erts/emulator/beam/erl_init.c @@ -436,7 +436,7 @@ static void load_preloaded(void) { int i; - int res; + Eterm res; Preload* preload_p; Eterm module_name; byte* code; @@ -455,8 +455,9 @@ load_preloaded(void) name); res = erts_load_module(NULL, 0, NIL, &module_name, code, length); sys_preload_end(&preload_p[i]); - if (res < 0) - erl_exit(1,"Failed loading preloaded module %s\n", name); + if (res != NIL) + erl_exit(1,"Failed loading preloaded module %s (%T)\n", + name, res); i++; } } diff --git a/erts/emulator/beam/global.h b/erts/emulator/beam/global.h index e4e9386dd7..6c75c3876b 100644 --- a/erts/emulator/beam/global.h +++ b/erts/emulator/beam/global.h @@ -866,7 +866,7 @@ typedef struct { Eterm* fname_ptr; /* Pointer to fname table */ } FunctionInfo; -int erts_load_module(Process *c_p, ErtsProcLocks c_p_locks, +Eterm erts_load_module(Process *c_p, ErtsProcLocks c_p_locks, Eterm group_leader, Eterm* mod, byte* code, int size); void init_load(void); BeamInstr* find_function_from_pc(BeamInstr* pc); -- cgit v1.2.3