godot/core/math/bvh_pair.inc
HP van Braam 240f510fa7 Core ubsan fixes
This fixes UBSAN errors reported by running our testsuite, importing the
TPS demo, and running the TPS demo. I have tried, wherever possible, to
fix issues related to reported issues but not directly reported by UBSAN
because thse code paths just happened to not have been exercised in
these cases.

These fixes apply only to errors reported, and caused by, core/

The following things have been changed:

* Make sure there are no implicit sign changing casts in core.
* Explicitly type enums that are part of a public API such that users of
  the API cannot pass in wrongly-sized values leading to potential stack
  corruption.
* Ensure that memcpy is never called with invalid or null pointers as
  this is undefined behavior, and when the engine is built with
  optimizations turned on leads to memory corruption and hard to debug
  crashes.
* Replace enum values only used as static values with constexpr static
  const values instead. This has no runtime overhead. This makes it so
  that the size of the enums is explicit.
* Make sure that nan and inf is handled consistently in String.
* Implement a _to_int template to ensure that all of the paths use the
  same algorhithm, and correct the negative integer case.
* Changed the way the json serializer precision work, and added tests to
  verify the new behavior. The behavior doesn't quite match master in
  particulary for negative doubles as the original code tried to cast -inf
  to an int. This then led to negative doubles losing all but one of
  their decimal points when serializing. Behavior in GDScript remains
  unchanged.
2024-12-18 14:31:12 +01:00

72 lines
1.6 KiB
C++

public:
// note .. maybe this can be attached to another node structure?
// depends which works best for cache.
struct ItemPairs {
struct Link {
void set(BVHHandle h, void *ud) {
handle = h;
userdata = ud;
}
BVHHandle handle;
void *userdata;
};
void clear() {
num_pairs = 0;
extended_pairs.reset();
expanded_aabb = BOUNDS();
}
BOUNDS expanded_aabb;
// maybe we can just use the number in the vector TODO
int32_t num_pairs;
LocalVector<Link> extended_pairs;
void add_pair_to(BVHHandle h, void *p_userdata) {
Link temp;
temp.set(h, p_userdata);
extended_pairs.push_back(temp);
num_pairs++;
}
uint32_t find_pair_to(BVHHandle h) const {
for (int n = 0; n < num_pairs; n++) {
if (extended_pairs[n].handle == h) {
return n;
}
}
return uint32_t(-1);
}
bool contains_pair_to(BVHHandle h) const {
return find_pair_to(h) != BVHCommon::INVALID;
}
// return success
void *remove_pair_to(BVHHandle h) {
void *userdata = nullptr;
for (int n = 0; n < num_pairs; n++) {
if (extended_pairs[n].handle == h) {
userdata = extended_pairs[n].userdata;
extended_pairs.remove_at_unordered(n);
num_pairs--;
break;
}
}
return userdata;
}
// experiment : scale the pairing expansion by the number of pairs.
// when the number of pairs is high, the density is high and a lower collision margin is better.
// when there are few local pairs, a larger margin is more optimal.
real_t scale_expansion_margin(real_t p_margin) const {
real_t x = real_t(num_pairs) * (1.0 / 9.0);
x = MIN(x, 1.0);
x = 1.0 - x;
return p_margin * x;
}
};