// Used to test clone/clone3 when setting CLONE_VM staticint child_fn_CLONE_VM(void* i_ptr) {
*reinterpret_cast<int*>(i_ptr) = 42; return123;
}
#ifdefined(__BIONIC__) staticvoid align_clone3_stack(clone_args& cl_args) { // Ensure that both the top and bottom of the child stack are 16-byte // aligned. The `clone3` system call expects that the provided pointer // refers to the lowest byte in the stack area.
uint64_t stack_ptr_misalignment = cl_args.stack & 0xf; if (stack_ptr_misalignment != 0) {
uint64_t alignment_offset = 16 - stack_ptr_misalignment;
cl_args.stack += alignment_offset;
cl_args.stack_size -= alignment_offset;
cl_args.stack_size &= ~0xf;
}
}
// Used to test clone/clone3 when not setting CLONE_VM. Useful for testing the // argument juggling code independent of virtual memory shenanigans. staticint child_fn_no_CLONE_VM(void* arg) { if (reinterpret_cast<uintptr_t>(arg) == 42) { return130;
} else { return140;
}
} #endif
int i = 0;
pid_t tid = clone(child_fn_CLONE_VM, &child_stack[1024], CLONE_VM, &i);
int status;
ASSERT_EQ(tid, TEMP_FAILURE_RETRY(waitpid(tid, &status, __WCLONE)));
ASSERT_EQ(42, i);
ASSERT_TRUE(WIFEXITED(status));
ASSERT_EQ(123, WEXITSTATUS(status)); #else // For glibc, any call to clone with CLONE_VM set will cause later pthread // calls in the same process to misbehave. // See https://sourceware.org/bugzilla/show_bug.cgi?id=10311 for more details.
GTEST_SKIP() << "glibc is broken"; #endif
}
TEST(sched, clone_errno) { // Check that our hand-written clone assembler sets errno correctly on failure.
uintptr_t fake_child_stack[16]; // If CLONE_THREAD is set, CLONE_SIGHAND must be set too.
ASSERT_ERRNO_FAILURE(EINVAL, -1,
clone(child_fn_CLONE_VM, &fake_child_stack[16], CLONE_THREAD, nullptr));
}
TEST(sched, clone_null_child_stack) { int i = 0;
ASSERT_ERRNO_FAILURE(EINVAL, -1, clone(child_fn_CLONE_VM, nullptr, CLONE_VM, &i));
}
errno = 0; // If CLONE_THREAD is set, CLONE_SIGHAND must be set too.
ASSERT_ERRNO_FAILURE(EINVAL, -1, clone3(&cl_args, sizeof(struct clone_args), nullptr, nullptr)); #else
GTEST_SKIP() << "glibc does not export a clone3 wrapper"; #endif
}
TEST(sched, clone3_null_child_stack) { #ifdefined(__BIONIC__)
clone_args cl_args = {};
errno = 0;
ASSERT_ERRNO_FAILURE(EINVAL, -1,
clone3(&cl_args, sizeof(struct clone_args), child_fn_no_CLONE_VM, nullptr)); #else
GTEST_SKIP() << "glibc does not export a clone3 wrapper"; #endif
}
TEST(sched, sched_getscheduler_sched_setscheduler) { // POSIX: "If pid is zero, the scheduling policy shall be returned for the // calling process".
ASSERT_EQ(sched_getscheduler(getpid()), sched_getscheduler(0));
ASSERT_EQ(0, sched_getparam(getpid(), &p));
ASSERT_EQ(original_policy, sched_setscheduler(getpid(), SCHED_BATCH, &p)); // POSIX says this should return the previous policy (here SCHED_BATCH), // but the Linux system call doesn't, and the glibc wrapper doesn't correct // this (the "returns 0" behavior is even documented on the man page in // the BUGS section). This was our historical behavior too, so in the // absence of reasons to break compatibility with ourselves and glibc, we // don't behave as POSIX specifies. http://b/26203902.
ASSERT_EQ(0, sched_setscheduler(getpid(), original_policy, &p));
}
TEST(sched, sched_getaffinity_failure) { // Trivial test of the errno-preserving/returning behavior. #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wnonnull"
ASSERT_ERRNO_FAILURE(EINVAL, -1, sched_getaffinity(getpid(), 0, nullptr)); #pragma clang diagnostic pop
}
TEST(sched, sched_setaffinity_failure) { // Trivial test of the errno-preserving/returning behavior. #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wnonnull"
ASSERT_ERRNO_FAILURE(EINVAL, -1, sched_setaffinity(getpid(), 0, nullptr)); #pragma clang diagnostic pop
}
TEST(sched, sched_setaffinity) {
cpu_set_t set;
CPU_ZERO(&set);
ASSERT_EQ(0, sched_getaffinity(getpid(), sizeof(set), &set)); // It's hard to make any more general claim than this, // but it ought to be safe to ask for the same affinity you already have.
ASSERT_EQ(0, sched_setaffinity(getpid(), sizeof(set), &set));
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.