Skip to content

Commit

Permalink
Fix non-opt C++ tests
Browse files Browse the repository at this point in the history
Summary: Mostly UBSAN fixes plus unlimited stack size for use with ASAN.

Reviewed By: alexmalyshev

Differential Revision: D56214641

fbshipit-source-id: b142849d21bdad41fcfb655502b9e15881cef696
  • Loading branch information
jbower-fb authored and facebook-github-bot committed Apr 19, 2024
1 parent a440344 commit 8b9c46f
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 7 deletions.
8 changes: 8 additions & 0 deletions cinderx/RuntimeTests/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
#include "tools/cxx/Resources.h"
#endif

#include <sys/resource.h>

#include <cstdlib>
#include <cstring>
#include <iostream>
Expand Down Expand Up @@ -188,6 +190,12 @@ int main(int argc, char* argv[]) {
// Prevent any test failures due to transient pointer values.
jit::setUseStablePointers(true);

// Particularly with ASAN, we might need a really large stack size.
struct rlimit rl;
rl.rlim_cur = RLIM_INFINITY;
rl.rlim_max = RLIM_INFINITY;
setrlimit(RLIMIT_STACK, &rl);

int result = RUN_ALL_TESTS();

PyMem_RawFree(argv0);
Expand Down
9 changes: 3 additions & 6 deletions cinderx/StaticPython/_static.c
Original file line number Diff line number Diff line change
Expand Up @@ -1340,7 +1340,6 @@ _static___build_cinder_class__(PyObject *self, PyObject *const *args, Py_ssize_t

// remove the kwarg dict and add the kwargs
PyObject *call_args[kwarg_count + nargs - 1];
PyObject *call_names[kwarg_count];
call_args[0] = args[0]; // func
call_args[1] = args[1]; // name

Expand All @@ -1349,17 +1348,15 @@ _static___build_cinder_class__(PyObject *self, PyObject *const *args, Py_ssize_t
call_args[i - extra_args] = args[i];
}

if (mkw != NULL) {
PyObject *call_names_tuple = NULL;
if (mkw != NULL && kwarg_count != 0) {
PyObject *call_names[kwarg_count];
Py_ssize_t i = 0, cur = 0;
PyObject *key, *value;
while (PyDict_Next(mkw, &i, &key, &value)) {
call_args[nargs - extra_args + cur] = value;
call_names[cur++] = key;
}
}

PyObject *call_names_tuple = NULL;
if (kwarg_count != 0) {
call_names_tuple = _PyTuple_FromArray(call_names, kwarg_count);
if (call_names_tuple == NULL) {
goto error;
Expand Down
2 changes: 1 addition & 1 deletion cinderx/StrictModules/Objects/callable_wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ class PythonWrappedCallableByName {
const std::vector<std::shared_ptr<BaseStrictObject>>& args,
const CallerContext& caller,
std::index_sequence<Is...>) {
Ref<> pyArgs[args.size()];
Ref<> pyArgs[args.size() < 1 ? 1 : args.size()];

for (std::size_t i = 0; i < args.size(); ++i) {
Ref<> val = args[i]->getPyObject();
Expand Down
18 changes: 18 additions & 0 deletions cinderx/StrictModules/Objects/numerics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -406,12 +406,21 @@ std::shared_ptr<BaseStrictObject> StrictInt::int__floordiv__(
return NotImplemented();
}

// This function and its use is to keep UBSAN quiet.
template <typename T>
static bool shift_more_than_64bits(T shift_amount) {
return shift_amount <= -64 || shift_amount >= 64;
}

std::shared_ptr<BaseStrictObject> StrictInt::int__lshift__(
std::shared_ptr<StrictInt> self,
const CallerContext& caller,
std::shared_ptr<BaseStrictObject> rhs) {
auto rhsInt = std::dynamic_pointer_cast<StrictInt>(rhs);
if (rhsInt) {
if (shift_more_than_64bits(rhsInt->value_)) {
return caller.makeInt(0);
}
INT_OP_BOTH_VALUE(self, rhsInt, caller.makeInt, <<, PyNumber_Lshift);
}
return NotImplemented();
Expand Down Expand Up @@ -456,6 +465,9 @@ std::shared_ptr<BaseStrictObject> StrictInt::int__rshift__(
std::shared_ptr<BaseStrictObject> rhs) {
auto rhsInt = std::dynamic_pointer_cast<StrictInt>(rhs);
if (rhsInt) {
if (shift_more_than_64bits(rhsInt->value_)) {
return caller.makeInt(0);
}
INT_OP_BOTH_VALUE(self, rhsInt, caller.makeInt, >>, PyNumber_Rshift);
}
return NotImplemented();
Expand Down Expand Up @@ -541,6 +553,9 @@ std::shared_ptr<BaseStrictObject> StrictInt::int__rlshift__(
std::shared_ptr<BaseStrictObject> lhs) {
auto lhsInt = std::dynamic_pointer_cast<StrictInt>(lhs);
if (lhsInt) {
if (shift_more_than_64bits(self->value_)) {
return caller.makeInt(0);
}
INT_OP_BOTH_VALUE(lhsInt, self, caller.makeInt, <<, PyNumber_Lshift);
}
return NotImplemented();
Expand Down Expand Up @@ -585,6 +600,9 @@ std::shared_ptr<BaseStrictObject> StrictInt::int__rrshift__(
std::shared_ptr<BaseStrictObject> lhs) {
auto lhsInt = std::dynamic_pointer_cast<StrictInt>(lhs);
if (lhsInt) {
if (shift_more_than_64bits(self->value_)) {
return caller.makeInt(0);
}
INT_OP_BOTH_VALUE(lhsInt, self, caller.makeInt, >>, PyNumber_Rshift);
}
return NotImplemented();
Expand Down

0 comments on commit 8b9c46f

Please sign in to comment.