Skip to content

[DO NOT MERGE] All of the changes we made for the ff integration#544

Draft
randomPoison wants to merge 8 commits intofw/fffrom
legare/ff
Draft

[DO NOT MERGE] All of the changes we made for the ff integration#544
randomPoison wants to merge 8 commits intofw/fffrom
legare/ff

Conversation

@randomPoison
Copy link
Contributor

No description provided.

* Differentiate between decls that shouldn't be rewritten vs ones that should not be analyzed at all.
* Only rewrite files files in the rewrite set when in library only mode.
* Avoid overwriting unchanged files when copying files into the rewrite dir. This helps avoid needing to recompile everything after running the rewriter.
* Add `extern "C"` blocks around various definitions so that they can be included in C++ code.
* Fix various places where C++ semantics don't line up with our C code, e.g. C allows implicit casts between pointer types, but in C++ we need to cast to `char*` manually.
* Other changes that I don't remember the rational for.
@ayrtonm ayrtonm changed the base branch from main to fw/ff May 2, 2025 18:43
}
printf("TLS segment of %s is not padded\n", libname);
exit(-1);
// exit(-1);
Copy link
Contributor

Choose a reason for hiding this comment

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

Did we end up needing this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, currently our various libs are not padded, so this causes the ff to exit. We could probably start padding those and then we can put this exit() back.

Comment on lines +437 to +467
#ifdef IA2_NO_DEFS
#define _IA2_INIT_RUNTIME(n) \
__attribute__((visibility("default"))) extern int ia2_n_pkeys_to_alloc; \
__attribute__((visibility("default"))) extern __thread void *ia2_stackptr_0[] \
__attribute__((aligned(4096))); \
\
REPEATB(n, declare_init_tls_fn, nop_macro); \
\
/* Returns `&ia2_stackptr_N` given a pkru value for the Nth compartment. */ \
__attribute__((visibility("default"))) void **ia2_stackptr_for_pkru(uint32_t pkru) { \
REPEATB(n, return_stackptr_if_compartment, \
return_stackptr_if_compartment); \
return NULL; \
} \
\
__attribute__((visibility("default"))) __attribute__((weak)) void init_stacks_and_setup_tls(void) { \
verify_tls_padding(); \
COMPARTMENT_SAVE_AND_RESTORE(REPEATB(n, ALLOCATE_COMPARTMENT_STACK_AND_SETUP_TLS, nop_macro), n); \
/* allocate an unprotected stack for the untrusted compartment */ \
allocate_stack_0(); \
} \
\
__attribute__((constructor)) static void ia2_init(void) { \
/* Set up global resources. */ \
ia2_set_up_tags(&ia2_n_pkeys_to_alloc); \
/* Initialize stacks for the main thread/ */ \
init_stacks_and_setup_tls(); \
REPEATB##n(setup_destructors_for_compartment, nop_macro); \
mark_init_finished(); \
}
#else
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@ayrtonm @fw-immunant Do either of you remember what this was about? We don't seem to be using IA2_NO_DEFS anywhere else, so I'm guessing it's not relevant anymore.

This fixes an issue if ff where we would hit a compartment violation inside of static constructors. When we loading libxul at startup, the init_pkey would get called and would mark the global memory as part of compartment 1, and then would set the pkey to 0. Then the static constructor would run and hit a compartment violation for trying to access compartment 1 memory from compartment 0. This change clears the pkru after initialization, allowing the static constructor to access compartment 1 memory.

This is basically the same fix as #561.
By default the thread isolated pools only get ~256 Mb of address space, whereas the normal pools get ~18 Gb. Since the thread isolated pools are the only ones used when compartmentalized, this caused Firefox to quickly run out of address space and hit OOM crashes.
Without explicitly adding `visibility("default")` the symbols were coming up as hidden in ff. This is the same issue we ran into elsewhere where we had to explicility mark visibility.

Firefox also has some cases where it uses `strdup` to copy a string and then tries to access that string from within libjpeg (via environment variables), so I've added `shared_strdup`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants