Skip to content

PR#5115: harden all byterun/fail.c exceptions against caml_global_data#947

Merged
gasche merged 1 commit intoocaml:trunkfrom
gasche:protect-caml_global_data
Dec 28, 2016
Merged

PR#5115: harden all byterun/fail.c exceptions against caml_global_data#947
gasche merged 1 commit intoocaml:trunkfrom
gasche:protect-caml_global_data

Conversation

@gasche
Copy link
Member

@gasche gasche commented Dec 3, 2016

In SVN commit 10793 ( git commit
f67d5c8 ), the bytecode runtime
implementations of caml_{failwith,invalid_argument} were hardened to
work when caml_global_data was not yet initialized. This is required
as those exception-raising functions are called by demarshalling
routines in intern.c, and could be called from there during
caml_global_data initialization by the bytecode runtime.

However, the code of intern.c also contains calls to other
exception-raising functions such as, currently,
caml_raise_out_of_memory and caml_end_of_file. This change defensively
protects all accesses to caml_global_data in byterun/fail.c.

(Only the bytecode versions of caml_raise_* are changed, there is no
difference for the native runtime.)

Copy link
Contributor

@xavierleroy xavierleroy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apart from those two details, I'm OK with these changes.

byterun/fail.c Outdated

CAMLexport void caml_array_bound_error(void)
{
check_global_data_param("Invalid_argument", "index out of bounds");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check is redundant with the one done in caml_invalid_argument.

byterun/fail.c Outdated
if (caml_global_data == 0) {
fprintf(stderr, "Fatal error: caml_is_special_exception\n");
exit(2);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better to return 0 in case caml_global_data == 0. Reason: caml_is_special_exception is used only in caml_format_exception to produce a more readable textual representation of some exceptions. It is better to fall back to the general, less readable representation than to abort with a fatal error.

@gasche gasche force-pushed the protect-caml_global_data branch from 14d7499 to 1cd79f7 Compare December 4, 2016 14:54
@gasche
Copy link
Member Author

gasche commented Dec 4, 2016

Thanks, I updated the PR. I will let the continuous integration proceed, then probably merge it.

(I checked that turning either of these functions' checks to always-fail breaks the build of the compiler distribution or the testsuite.)

@mshinwell
Copy link
Contributor

@gasche Can you rebase this and merge?

@mshinwell mshinwell added the bug label Dec 27, 2016
@mshinwell mshinwell added this to the 4.05.0 milestone Dec 27, 2016
@gasche gasche force-pushed the protect-caml_global_data branch from 1cd79f7 to 068745d Compare December 28, 2016 05:29
In SVN commit 10793 ( git commit
f67d5c8 ), the bytecode runtime
implementations of caml_{failwith,invalid_argument} were hardened to
work when caml_global_data was not yet initialized. This is required
as those exception-raising functions are called by demarshalling
routines in intern.c, and could be called from there during
caml_global_data initialization by the bytecode runtime.

However, the code of intern.c also contains calls to other
exception-raising functions such as, currently,
caml_raise_out_of_memory and caml_end_of_file. This change defensively
protects all accesses to caml_global_data in byterun/fail.c.

(Only the bytecode versions of caml_raise_* are changed, there is no
difference for the native runtime.)
@gasche gasche merged commit 4a758ce into ocaml:trunk Dec 28, 2016
camlspotter pushed a commit to camlspotter/ocaml that referenced this pull request Oct 17, 2017
PR#5115: harden all byterun/fail.c exceptions against caml_global_data
stedolan pushed a commit to stedolan/ocaml that referenced this pull request Mar 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants