Skip to content
Open
Show file tree
Hide file tree
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
15 changes: 11 additions & 4 deletions include/mdsobjects.h
Original file line number Diff line number Diff line change
Expand Up @@ -2892,13 +2892,17 @@ namespace MDSplus
dtype = DTYPE_DICTIONARY;
}

// A NULL value represents missing ("*"): it is what execute() returns
// for an expression that evaluates to nil, and convertToApdDsc() emits
// it as a null descriptor pointer
Data *getItem(String *strData)
{
for (std::size_t i = 0; i < descs.size(); i += 2)
{
if (strData->equals(descs[i]))
{
descs[i + 1]->incRefCount();
if (descs[i + 1])
descs[i + 1]->incRefCount();
return descs[i + 1];
}
}
Expand All @@ -2915,17 +2919,20 @@ namespace MDSplus
descs[i] = strData;
strData->incRefCount();

descs[i + 1]->decRefCount();
if (descs[i + 1])
descs[i + 1]->decRefCount();
descs[i + 1] = data;
data->incRefCount();
if (data)
data->incRefCount();
return;
}
}

descs.push_back(strData);
descs.push_back(data);
strData->incRefCount();
data->incRefCount();
if (data)
data->incRefCount();
}

std::size_t len() { return Apd::len() / 2; }
Expand Down
5 changes: 5 additions & 0 deletions mdsobjects/cpp/mdsdataobjects.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,11 @@ Data::~Data()

void MDSplus::deleteData(Data *data)
{
// A NULL Data represents missing ("*"). deleteData() must accept it as a
// no-op: RAII wrappers (AutoData) and Apd::propagateDeletion() hand it the
// NULL that getItem()/execute() return for a nil result.
if (!data)
return;
if (data->refCount <= 1)
{
if (data->units)
Expand Down
1 change: 1 addition & 0 deletions mdsobjects/cpp/testing/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ set(_test_source_list
MdsEventSuppression.cpp
MdsEventTest.cpp
MdsConnectionTest.cpp
MdsGetManyTest.cpp

# SKIP_TEST is not supported
# MdsCallTest.cpp
Expand Down
137 changes: 137 additions & 0 deletions mdsobjects/cpp/testing/MdsGetManyTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/*
Copyright (c) 2026, Massachusetts Institute of Technology All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdlib.h>

#include <mdsdescrip.h>
#include <mdsobjects.h>

#include "testing.h"
#include "testutils/unique_ptr.h"

using namespace MDSplus;
using namespace testing;

// The mdsip server entry point for Connection.getMany() batches
// (tdi/remote/GetManyExecute.fun -> MdsObjectsCppShr->GetManyExecute)
extern "C" struct descriptor_xd *GetManyExecute(char *serializedIn);

////////////////////////////////////////////////////////////////////////////////
// GetMany results that evaluate to missing (issue #3070) ////////////////////
////////////////////////////////////////////////////////////////////////////////

int main()
{
BEGIN_TESTING(GetMany);

{ // A Dictionary must accept a missing (NULL) value: getManyObj() stores
// one whenever a query succeeds with a nil result. The rest of the Apd
// machinery (constructor, propagateDeletion, convertToApdDsc) already
// NULL-guards its elements.
unique_ptr<Dictionary> dict = new Dictionary();
String *key = new String("value"); // freed by the Dictionary destructor
String lookup("value");

dict->setItem(key, NULL);
TEST1(dict->len() == 1);
TEST1(dict->getItem(&lookup) == NULL);

// replace the missing value with data...
Int32 *num = new Int32(42);
dict->setItem(key, num);
unique_ptr<Data> value = dict->getItem(&lookup);
TEST1(value->getInt() == 42);

// ...and the data with a missing value again
dict->setItem(key, NULL);
TEST1(dict->len() == 1);
TEST1(dict->getItem(&lookup) == NULL);
deleteData(num);
}

{ // A missing value must survive the serialize / deserialize round trip
// used to transport the GetManyExecute() result dictionary.
unique_ptr<Dictionary> dict = new Dictionary();
dict->setItem(new String("value"), NULL);

int size = 0;
AutoArray<char> serialized(dict->serialize(&size));
TEST1(size > 0);

unique_ptr<Dictionary> back = (Dictionary *)deserialize(serialized.get());
String lookup("value");
TEST1(back->len() == 1);
TEST1(back->getItem(&lookup) == NULL);
}

{ // End to end: a GetMany query that SUCCEEDS with a nil result must come
// back as { 'value': missing }, matching what a plain get() of the same
// expression returns, instead of crashing the mdsip worker.
// The expression mirrors GETNCI(<node>, "RECORD") of a clock whose
// record is a Range with a missing begin: * : * : 5E-6
Dictionary *nilQuery = new Dictionary(); // freed by the List destructor
nilQuery->setItem(new String("name"), new String("nil"));
nilQuery->setItem(new String("exp"),
new String("DATA(BEGIN_OF(BUILD_RANGE(*, *, 5E-6)))"));

Dictionary *numQuery = new Dictionary();
numQuery->setItem(new String("name"), new String("num"));
numQuery->setItem(new String("exp"), new String("1 + 1"));

unique_ptr<List> queries = new List();
queries->append(nilQuery);
queries->append(numQuery);

int serSize = 0;
AutoArray<char> serialized(queries->serialize(&serSize));

struct descriptor_xd *outXd = GetManyExecute(serialized.get());
TEST1(outXd != NULL && outXd->pointer != NULL);

// GetManyExecute() returns the result dictionary serialized into a
// byte array, just as it goes over the wire
struct descriptor_a *bytes = (struct descriptor_a *)outXd->pointer;
unique_ptr<Dictionary> result =
(Dictionary *)deserialize((char *)bytes->pointer);

String nilName("nil"), numName("num"), valueKey("value"), errorKey("error");

{ // sanity: the numeric query evaluated normally
unique_ptr<Dictionary> answer = (Dictionary *)result->getItem(&numName);
TEST1((Dictionary *)answer != NULL);
unique_ptr<Data> value = answer->getItem(&valueKey);
TEST1(value->getInt() == 2);
}

{ // the nil query: answered, not an error, and the value is missing
unique_ptr<Dictionary> answer = (Dictionary *)result->getItem(&nilName);
TEST1((Dictionary *)answer != NULL);
TEST1(answer->getItem(&errorKey) == NULL);
TEST1(answer->len() == 1); // exactly one entry: 'value'
TEST1(answer->getItem(&valueKey) == NULL); // ...holding missing
}
}

END_TESTING;
}
10 changes: 9 additions & 1 deletion testing/backends/check/lib/macos_timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include <sys/errno.h>
#include <stdlib.h>
#include <time.h>
#include <signal.h>
#include <unistd.h>

#include <dispatch/dispatch.h>

Expand Down Expand Up @@ -56,7 +58,13 @@ _timer_handler(void *arg)
static inline void _default_timer_expiration(union sigval sv)
{
(void) sv;
signal(SIGALRM, NULL);
/* When timer_create() is called with a NULL sigevent (as the test
* harness does), a POSIX timer would deliver SIGALRM to the process on
* expiration. Emulate that here so the harness' SIGALRM handler runs and
* kills the timed-out child. The previous signal(SIGALRM, NULL) only reset
* the SIGALRM disposition and never raised it, so test timeouts silently
* never fired on macOS and a hung/crashed child stalled the runner. */
kill(getpid(), SIGALRM);
}

static inline int
Expand Down
19 changes: 19 additions & 0 deletions testing/check_backend.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ extern char *strsignal(int);
#include <sys/wait.h>
#endif

#ifdef __APPLE__
#include <mach/mach.h>
#endif

#include <check.h>
#include <check_list.h>
#include <check_impl.h>
Expand Down Expand Up @@ -758,6 +762,21 @@ int __setup_child()
{
setpgid(0, 0);
group_pid = getpgrp();

#ifdef __APPLE__
// On macOS a hardware fault (null deref, bad instruction, ...) is caught
// by the task-level Mach exception port (the system crash reporter), which
// parks the faulting thread instead of terminating the process. The child
// then never dies, the parent blocks forever in waitpid(), and the whole
// run stalls. Clearing the exception ports lets the fault fall through to
// the default action, so a crashing test dies promptly and is reported,
// just as on Linux.
task_set_exception_ports(mach_task_self(),
EXC_MASK_BAD_ACCESS | EXC_MASK_BAD_INSTRUCTION |
EXC_MASK_ARITHMETIC,
MACH_PORT_NULL, EXCEPTION_DEFAULT, 0);
#endif

return 1;
}
#endif
Expand Down