This is an archive of the discontinued LLVM Phabricator instance.

[scudo] Implement Fuchsia backend for the new MemMap API
ClosedPublic

Authored by fabio-d on Jun 27 2023, 9:04 AM.

Diff Detail

Event Timeline

fabio-d created this revision.Jun 27 2023, 9:04 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 27 2023, 9:04 AM
fabio-d requested review of this revision.Jun 27 2023, 9:04 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 27 2023, 9:04 AM
Herald added a subscriber: Restricted Project. · View Herald Transcript
Chia-hungDuan added inline comments.Jun 27 2023, 9:46 AM
compiler-rt/lib/scudo/standalone/mem_map_fuchsia.cpp
24

nit: Suggest moving static functions (here and below) outside scudo namespace.

69–71

nit:single statement

94–96

nit:single statement

118–120

nit:single statement

169–171

nit:single statement

180

Do you want to handle MAP_ALLOWNOMEM here?

189–191

nit: omit curly brackets if it's single statement

195–197

nit: single statement

218–220

nit: single statement

fabio-d updated this revision to Diff 535338.Jun 28 2023, 5:13 AM
fabio-d updated this revision to Diff 535344.Jun 28 2023, 5:34 AM
fabio-d marked 6 inline comments as done.
fabio-d marked an inline comment as done.Jun 28 2023, 5:38 AM
fabio-d added inline comments.
compiler-rt/lib/scudo/standalone/mem_map_fuchsia.cpp
24

What goal do you have in mind? I can do that, but I'm not sure it's more readable as I'll have to add the scudo:: prefix to all the references to other Scudo functions and types, e.g.

static void NORETURN dieOnError(zx_status_t Status, const char *FnName,
                                scudo::uptr Size) {
  char Error[128];
  scudo::formatString(Error, sizeof(Error),
                      "SCUDO ERROR: %s failed with size %zuKB (%s)", FnName,
                      Size >> 10, _zx_status_get_string(Status));
  scudo::outputRaw(Error);
  scudo::die();
}
180

Nice catch! The zx_vmar_map documentation says that userspace has no way to handle ZX_ERR_NO_MEMORY in a good way, and that it will become impossible to get this error in the future. Anyway, I guess that an extra check within an already-UNLIKELY block doesn't hurt, so I've added it.

For completeness, dispatch could also result in the same error (because of the zx_vmo_create call). In theory, I think we should make dispatch similarly fallible and with an option to pass MAP_ALLOWNOMEM. In practice, I think that it's OK to just die if dispatch fails, given who the current users are.

Caslyn accepted this revision.Jun 30 2023, 9:45 AM
Caslyn added inline comments.
compiler-rt/lib/scudo/standalone/mem_map_fuchsia.cpp
161

I recall the Size passed to decommit must be page aligned for the op to succeed - want to make sure, is that always the case here?

This revision is now accepted and ready to land.Jun 30 2023, 9:45 AM
fabio-d added inline comments.Jun 30 2023, 10:29 AM
compiler-rt/lib/scudo/standalone/mem_map_fuchsia.cpp
161

Good point! Actually, all the addresses, sizes and capacities must be page aligned.

@Chia-hungDuan I'd say that this and similar checks should be DCHECKs in mem_map_base.h, so that they are checked on Linux too. I think think we even discussed it, but there was some caveat that I don't remember about Trusty. I saw that Trusty's impl has changed since then. Would you see any issue in enforcing page alignment there too?

Chia-hungDuan accepted this revision.Jun 30 2023, 1:49 PM
Chia-hungDuan added inline comments.
compiler-rt/lib/scudo/standalone/mem_map_fuchsia.cpp
24

This is just a coding convention that I usually have in some llvm projects. Using anonymous namespace and static to manage the code that has the scope only in a compilation unit.

In general, we put local class declaration in anonymous namespace only. Other stuff will be done with static in global namespace.

In the case here, it makes the scudo namespace clear. Not a strong opinion. feel free to stick on your preference.

161

On Linux, it only requires the Size to be greater than 0. It does the round up stuff in the kernel. Luke said he would like to do the same thing in Fuchsia.

I think it's fine to add this constraint at one level above platform specific layer. I need to review the rationale of enforcing that (include Trusty's stuff)

This revision was automatically updated to reflect the committed changes.