RFR: 8286876: NMT.test_unaliged_block_address_vm_assert fails if using clang toolchain [v6]
Albert Mingkun Yang
ayang at openjdk.org
Mon Jan 16 23:18:17 UTC 2023
On Mon, 16 Jan 2023 19:08:11 GMT, Thomas Stuefe <stuefe at openjdk.org> wrote:
>> Good question. Perhaps in this specific case we can throw away the const information, but it's difficult to discover whether that's true and doesn't look correct to a reader at a first glance. I think an extra method reduces reading complexity such that it is worth keeping.
>
> You need both because you should be able to resolve a MallocHeader from a const payload pointer, but that should yield a const pointer too, not a non-const one. Yes, you could return a non-const header pointer, but that would be just bad const discipline.
The input arg should be `const void*`, which accepts both `void*` and `const void*`, because that's a promise to the caller that the current method will not mutate the mem pointed by the input arg. (Ofc, one could throw away `const` and mutate the mem anyway inside the method, but that's just bad code.)
> but that should yield a const pointer too, not a non-const one.
I believe your reasoning is that in&out pointers are essentially the same (off by 1), so they should have the same const-ness.
However, the in&out args are two semantically different entities, `void*` vs `MallocHeader*`. For instance, a caller may construct a `MallocHeader*` out of a `const void*` -- the mem is immutable via `void*` but mutable via `MallocHeader*` due to the extra info offered by the more concrete type. IOW, whether the mem indicated by `MallocHeader*` is mutable or not is up to the caller.
Looking at the `const`-variant caller (I can just find this single one), it's more or less the same as a caller of the non-`const` variant.
static void check_expected_malloc_header(const void* payload, MEMFLAGS type, size_t size) {
const MallocHeader* hdr = MallocHeader::resolve_checked(payload);
EXPECT_EQ(hdr->size(), size);
EXPECT_EQ(hdr->flags(), type);
}
vs
void* const memblock = ...
MallocHeader* header2 = MallocHeader::resolve_checked(memblock);
assert(header2->size() == size, "Wrong size");
assert(header2->flags() == flags, "Wrong flags");
(Seems to me, `header2` should be `const` as well -- here we promise not to mutate the obj via `header2`.)
-------------
PR: https://git.openjdk.org/jdk/pull/11465
More information about the hotspot-runtime-dev
mailing list