Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion test/core/slice/slice_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -237,13 +237,37 @@ static void test_slice_from_copied_string_works(void) {
grpc_slice_unref(slice);
}

// optimizer doesn't see inside this function
void print_slice(grpc_slice slice)
{
gpr_log(GPR_ERROR, "slice start ptr: %p", GRPC_SLICE_START_PTR(slice));
}

static void test_slice_interning(void) {
LOG_TEST_NAME("test_slice_interning");

grpc_init();
grpc_slice src1 = grpc_slice_from_copied_string("hello123456789123456789");
grpc_slice src2 = grpc_slice_from_copied_string("hello123456789123456789");
GPR_ASSERT(GRPC_SLICE_START_PTR(src1) != GRPC_SLICE_START_PTR(src2));

// the original assert checks that newly allocated slices point to different data
// GPR_ASSERT(GRPC_SLICE_START_PTR(src1) != GRPC_SLICE_START_PTR(src2));

if (GRPC_SLICE_START_PTR(src1) == GRPC_SLICE_START_PTR(src2)) {
gpr_log(GPR_ERROR, "assertion would have failed");
gpr_log(GPR_ERROR, "we just evaluated the start ptrs as being equal, but it's actually not true");
// we want to print the actual values of slice_start_ptr to catch the compiler red-handed
// but we need to be sneaky and look at the values from a different function (outside of the optimizer's sight),
// otherwise the internal state in the compiler changes and we won't trigger the suspected compiler bug.
print_slice(src1);
print_slice(src2);

// If you uncomment the next line, compiler will see that we're using the src1 value and
// the enclosing if statement will be evaluated correctly (pointers will be seen as not equal)
// gpr_log(GPR_ERROR, "src1 start ptr:", GRPC_SLICE_START_PTR(src1));
abort();
}

grpc_slice interned1 = grpc_slice_intern(src1);
grpc_slice interned2 = grpc_slice_intern(src2);
GPR_ASSERT(GRPC_SLICE_START_PTR(interned1) ==
Expand Down