From c5e3f3bfe3364c40c8c3d90c1ceb371f3eee4ce5 Mon Sep 17 00:00:00 2001 From: Ivan Popivanov Date: Mon, 3 Dec 2018 16:05:07 -0800 Subject: [PATCH 1/5] Avoid nullptr access --- src/Memory.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Memory.cpp b/src/Memory.cpp index f333d01f..79ef0675 100644 --- a/src/Memory.cpp +++ b/src/Memory.cpp @@ -37,6 +37,7 @@ void Memory::ResetThread(const unsigned thrId) void Memory::ReturnThread(const unsigned thrId) { + if(memory[thrId] == nullptr) return; memory[thrId]->transTable->ReturnAllMemory(); memory[thrId]->memUsed = Memory::MemoryInUseMB(thrId); } From a883ad11e1e7c592e16cd15af8b13b318d5e568c Mon Sep 17 00:00:00 2001 From: Ivan Popivanov Date: Sat, 8 Dec 2018 18:00:57 -0800 Subject: [PATCH 2/5] Added Json api for retrieving the double dummy table --- .gitmodules | 3 ++ include/dll.h | 4 ++ nlohmann-json | 1 + src/JsonApi.cpp | 83 +++++++++++++++++++++++++++++++++++++ src/Makefiles/depends_o.txt | 1 + src/Makefiles/sources.txt | 1 + src/Memory.cpp | 3 +- 7 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 .gitmodules create mode 160000 nlohmann-json create mode 100644 src/JsonApi.cpp diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 00000000..a22b5c45 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "nlohmann-json"] + path = nlohmann-json + url = https://github.com/nlohmann/json.git diff --git a/include/dll.h b/include/dll.h index c946e4a3..03357c79 100644 --- a/include/dll.h +++ b/include/dll.h @@ -532,4 +532,8 @@ EXTERN_C DLLEXPORT void STDCALL ErrorMessage( int code, char line[80]); +EXTERN_C DLLEXPORT char * STDCALL JsonApi_CalcAllTables(const char * params); + +EXTERN_C DLLEXPORT void STDCALL JsonApi_FreeCPtr(void * ptr); + #endif diff --git a/nlohmann-json b/nlohmann-json new file mode 160000 index 00000000..e73dfef6 --- /dev/null +++ b/nlohmann-json @@ -0,0 +1 @@ +Subproject commit e73dfef6e51b876cbfd8fadb8b6a1771a196b6e8 diff --git a/src/JsonApi.cpp b/src/JsonApi.cpp new file mode 100644 index 00000000..7e7e0b6a --- /dev/null +++ b/src/JsonApi.cpp @@ -0,0 +1,83 @@ +#include "json.hpp" + +#include "CalcTables.h" +#include "dds.h" + +using namespace std; +using json = nlohmann::json; + +static json TableResultToJson(const ddTableResults * table); + +char * STDCALL JsonApi_CalcAllTables(const char * params) +{ + auto jobj = json::parse(params); + + ddTableDealsPBN pbnDeals; + pbnDeals.noOfTables = 1; + strcpy(pbnDeals.deals[0].cards, jobj["pbn"].get().c_str()); + + ddTablesRes table; + allParResults pres; + + int mode = 0; // No par calculation + int trumpFilter[DDS_STRAINS] = {0, 0, 0, 0, 0}; // All + int res = CalcAllTablesPBN(&pbnDeals, 0, trumpFilter, &table, &pres); + + json ans; + + if (res != RETURN_NO_FAULT) + { + char line[80]; + ErrorMessage(res, line); + ans["error"] = line; + return strdup(ans.dump().c_str()); + } + + ans = TableResultToJson(&table.results[0]); + return strdup(ans.dump().c_str()); +} + +void STDCALL JsonApi_FreeCPtr(void * ptr) +{ + free(ptr); +} + +static json TableResultToJson(const ddTableResults * table) +{ + json jdir; + + jdir["s"] = table->resTable[0][0]; + jdir["h"] = table->resTable[1][0]; + jdir["d"] = table->resTable[2][0]; + jdir["c"] = table->resTable[3][0]; + jdir["n"] = table->resTable[4][0]; + + json ans; + ans["north"] = jdir; + + jdir["s"] = table->resTable[0][1]; + jdir["h"] = table->resTable[1][1]; + jdir["d"] = table->resTable[2][1]; + jdir["c"] = table->resTable[3][1]; + jdir["n"] = table->resTable[4][1]; + + ans["east"] = jdir; + + jdir["s"] = table->resTable[0][2]; + jdir["h"] = table->resTable[1][2]; + jdir["d"] = table->resTable[2][2]; + jdir["c"] = table->resTable[3][2]; + jdir["n"] = table->resTable[4][2]; + + ans["south"] = jdir; + + jdir["s"] = table->resTable[0][3]; + jdir["h"] = table->resTable[1][3]; + jdir["d"] = table->resTable[2][3]; + jdir["c"] = table->resTable[3][3]; + jdir["n"] = table->resTable[4][3]; + + ans["west"] = jdir; + + return ans; +} \ No newline at end of file diff --git a/src/Makefiles/depends_o.txt b/src/Makefiles/depends_o.txt index f705666f..cbff05b1 100644 --- a/src/Makefiles/depends_o.txt +++ b/src/Makefiles/depends_o.txt @@ -16,6 +16,7 @@ File.o: File.h Init.o: Init.h dds.h ../include/portab.h ../include/dll.h Memory.h Init.o: TransTable.h TransTableS.h TransTableL.h Moves.h File.h debug.h Init.o: System.h Scheduler.h TimeStatList.h TimeStat.h Timer.h +JsonApi.o: CalcTables.h dds.h ../include/portab.h ../include/dll.h LaterTricks.o: LaterTricks.h dds.h ../include/portab.h ../include/dll.h LaterTricks.o: Memory.h TransTable.h TransTableS.h TransTableL.h Moves.h LaterTricks.o: File.h debug.h diff --git a/src/Makefiles/sources.txt b/src/Makefiles/sources.txt index 566a0584..dcfd0506 100644 --- a/src/Makefiles/sources.txt +++ b/src/Makefiles/sources.txt @@ -7,6 +7,7 @@ SOURCE_FILES = \ DealerPar.cpp \ File.cpp \ Init.cpp \ + JsonApi.cpp \ LaterTricks.cpp \ Memory.cpp \ Moves.cpp \ diff --git a/src/Memory.cpp b/src/Memory.cpp index 79ef0675..59922be3 100644 --- a/src/Memory.cpp +++ b/src/Memory.cpp @@ -37,7 +37,7 @@ void Memory::ResetThread(const unsigned thrId) void Memory::ReturnThread(const unsigned thrId) { - if(memory[thrId] == nullptr) return; + if(memory[thrId] == nullptr || memory[thrId]->transTable == nullptr) return; memory[thrId]->transTable->ReturnAllMemory(); memory[thrId]->memUsed = Memory::MemoryInUseMB(thrId); } @@ -113,6 +113,7 @@ ThreadData * Memory::GetPtr(const unsigned thrId) double Memory::MemoryInUseMB(const unsigned thrId) const { + if(memory[thrId] == nullptr || memory[thrId]->transTable == nullptr) return -101; return memory[thrId]->transTable->MemoryInUse() + 8192. * sizeof(relRanksType) / static_cast(1024.); } From 31ca27bb98af086a32f0769ffa4c1e41c1010a03 Mon Sep 17 00:00:00 2001 From: Ivan Popivanov Date: Sat, 8 Dec 2018 18:39:42 -0800 Subject: [PATCH 3/5] master is the default branch for nlohmann/json --- .gitmodules | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitmodules b/.gitmodules index a22b5c45..c0b9ccbc 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,4 @@ [submodule "nlohmann-json"] path = nlohmann-json - url = https://github.com/nlohmann/json.git + url = https://github.com/nlohmann/json.git + branch = master From 2e388f2b05d2765159f25dc59cbf97f43d4b7074 Mon Sep 17 00:00:00 2001 From: Ivan Popivanov Date: Sat, 8 Dec 2018 20:20:00 -0800 Subject: [PATCH 4/5] switching to branch develop for nlohmann-json --- .gitmodules | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitmodules b/.gitmodules index c0b9ccbc..d5308f53 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,4 +1,4 @@ [submodule "nlohmann-json"] path = nlohmann-json url = https://github.com/nlohmann/json.git - branch = master + branch = develop From 61627195a91a06b2de849638cb56b806e98c5a97 Mon Sep 17 00:00:00 2001 From: Ivan Popivanov Date: Sun, 9 Dec 2018 02:50:58 -0800 Subject: [PATCH 5/5] Fixed the core dumps on Ubuntu --- .gitmodules | 4 - nlohmann-json | 1 - src/Init.cpp | 5 +- src/JsonApi.cpp | 131 ++++++++++++++++++---------- src/Makefiles/Makefile_linux_shared | 15 +++- src/Memory.cpp | 8 +- src/dds.cpp | 2 - 7 files changed, 109 insertions(+), 57 deletions(-) delete mode 160000 nlohmann-json diff --git a/.gitmodules b/.gitmodules index d5308f53..e69de29b 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,4 +0,0 @@ -[submodule "nlohmann-json"] - path = nlohmann-json - url = https://github.com/nlohmann/json.git - branch = develop diff --git a/nlohmann-json b/nlohmann-json deleted file mode 160000 index e73dfef6..00000000 --- a/nlohmann-json +++ /dev/null @@ -1 +0,0 @@ -Subproject commit e73dfef6e51b876cbfd8fadb8b6a1771a196b6e8 diff --git a/src/Init.cpp b/src/Init.cpp index 5c2fff0c..3aad3dbe 100644 --- a/src/Init.cpp +++ b/src/Init.cpp @@ -343,8 +343,9 @@ void InitConstants() groupData[ris].rank[g] = topBitNo; groupData[ris].sequence[g] = 0; groupData[ris].fullseq[g] = topBitRank; - groupData[ris].gap[g] = - topside[topBitNo] & botside[ groupData[ris].rank[g - 1] ]; + groupData[ris].gap[g] = (g == 0 ? + topside[topBitNo] & botside[ 0 ] : + topside[topBitNo] & botside[ groupData[ris].rank[g - 1] ]); } } } diff --git a/src/JsonApi.cpp b/src/JsonApi.cpp index 7e7e0b6a..3d3073ab 100644 --- a/src/JsonApi.cpp +++ b/src/JsonApi.cpp @@ -1,20 +1,24 @@ -#include "json.hpp" +#include +#include #include "CalcTables.h" #include "dds.h" using namespace std; -using json = nlohmann::json; +using namespace rapidjson; +// using json = nlohmann::json; -static json TableResultToJson(const ddTableResults * table); +static char * TableResultToJson(const ddTableResults * table); +static char * ErrorToJson(const char * error); char * STDCALL JsonApi_CalcAllTables(const char * params) { - auto jobj = json::parse(params); + Document jobj; + jobj.Parse(params); ddTableDealsPBN pbnDeals; pbnDeals.noOfTables = 1; - strcpy(pbnDeals.deals[0].cards, jobj["pbn"].get().c_str()); + strcpy(pbnDeals.deals[0].cards, jobj["pbn"].GetString()); ddTablesRes table; allParResults pres; @@ -23,18 +27,14 @@ char * STDCALL JsonApi_CalcAllTables(const char * params) int trumpFilter[DDS_STRAINS] = {0, 0, 0, 0, 0}; // All int res = CalcAllTablesPBN(&pbnDeals, 0, trumpFilter, &table, &pres); - json ans; - if (res != RETURN_NO_FAULT) { char line[80]; - ErrorMessage(res, line); - ans["error"] = line; - return strdup(ans.dump().c_str()); + ErrorMessage(res, line); + return ErrorToJson(line); } - ans = TableResultToJson(&table.results[0]); - return strdup(ans.dump().c_str()); + return TableResultToJson(&table.results[0]); } void STDCALL JsonApi_FreeCPtr(void * ptr) @@ -42,42 +42,85 @@ void STDCALL JsonApi_FreeCPtr(void * ptr) free(ptr); } -static json TableResultToJson(const ddTableResults * table) +static char * ErrorToJson(const char * error) { - json jdir; - - jdir["s"] = table->resTable[0][0]; - jdir["h"] = table->resTable[1][0]; - jdir["d"] = table->resTable[2][0]; - jdir["c"] = table->resTable[3][0]; - jdir["n"] = table->resTable[4][0]; - - json ans; - ans["north"] = jdir; - - jdir["s"] = table->resTable[0][1]; - jdir["h"] = table->resTable[1][1]; - jdir["d"] = table->resTable[2][1]; - jdir["c"] = table->resTable[3][1]; - jdir["n"] = table->resTable[4][1]; + StringBuffer sb; + Writer writer(sb); - ans["east"] = jdir; + writer.StartObject(); - jdir["s"] = table->resTable[0][2]; - jdir["h"] = table->resTable[1][2]; - jdir["d"] = table->resTable[2][2]; - jdir["c"] = table->resTable[3][2]; - jdir["n"] = table->resTable[4][2]; + writer.Key("error"); + writer.String(error); - ans["south"] = jdir; + writer.EndObject(); - jdir["s"] = table->resTable[0][3]; - jdir["h"] = table->resTable[1][3]; - jdir["d"] = table->resTable[2][3]; - jdir["c"] = table->resTable[3][3]; - jdir["n"] = table->resTable[4][3]; - - ans["west"] = jdir; + return strdup(sb.GetString()); +} - return ans; +static char * TableResultToJson(const ddTableResults * table) +{ + StringBuffer sb; + Writer writer(sb); + + writer.StartObject(); + + writer.Key("north"); + writer.StartObject(); + writer.Key("s"); + writer.Int(table->resTable[0][0]); + writer.Key("h"); + writer.Int(table->resTable[1][0]); + writer.Key("d"); + writer.Int(table->resTable[2][0]); + writer.Key("c"); + writer.Int(table->resTable[3][0]); + writer.Key("n"); + writer.Int(table->resTable[4][0]); + writer.EndObject(); + + writer.Key("east"); + writer.StartObject(); + writer.Key("s"); + writer.Int(table->resTable[0][1]); + writer.Key("h"); + writer.Int(table->resTable[1][1]); + writer.Key("d"); + writer.Int(table->resTable[2][1]); + writer.Key("c"); + writer.Int(table->resTable[3][1]); + writer.Key("n"); + writer.Int(table->resTable[4][1]); + writer.EndObject(); + + writer.Key("south"); + writer.StartObject(); + writer.Key("s"); + writer.Int(table->resTable[0][2]); + writer.Key("h"); + writer.Int(table->resTable[1][2]); + writer.Key("d"); + writer.Int(table->resTable[2][2]); + writer.Key("c"); + writer.Int(table->resTable[3][2]); + writer.Key("n"); + writer.Int(table->resTable[4][2]); + writer.EndObject(); + + writer.Key("west"); + writer.StartObject(); + writer.Key("s"); + writer.Int(table->resTable[0][3]); + writer.Key("h"); + writer.Int(table->resTable[1][3]); + writer.Key("d"); + writer.Int(table->resTable[2][3]); + writer.Key("c"); + writer.Int(table->resTable[3][3]); + writer.Key("n"); + writer.Int(table->resTable[4][3]); + writer.EndObject(); + + writer.EndObject(); + + return strdup(sb.GetString()); } \ No newline at end of file diff --git a/src/Makefiles/Makefile_linux_shared b/src/Makefiles/Makefile_linux_shared index ec4a2c8a..5b4d80f6 100644 --- a/src/Makefiles/Makefile_linux_shared +++ b/src/Makefiles/Makefile_linux_shared @@ -81,9 +81,18 @@ WARN_FLAGS = \ -Wno-long-long \ -Wno-format +# SANITIZE_FLAGS = \ +# -fsanitize=address \ +# -fsanitize=bounds \ +# -fsanitize=object-size \ +# -fno-omit-frame-pointer + +SANITIZE_FLAGS = + COMPILE_FLAGS = -fPIC -O3 -flto -fopenmp -mtune=generic -std=c++11 \ $(WARN_FLAGS) \ - $(DDS_BEHAVIOR) $(THREAD_COMPILE) $(THREADING) + $(DDS_BEHAVIOR) $(THREAD_COMPILE) $(THREADING) \ + $(SANITIZE_FLAGS) DLLBASE = dds SHARED_LIB = lib$(DLLBASE).so @@ -100,8 +109,8 @@ LINK_FLAGS = \ -Wl,-z \ -Wl,relro \ $(THREAD_LINK) \ - -fPIC - + -fPIC \ + $(SANITIZE_FLAGS) linux: $(O_FILES) $(CC) \ diff --git a/src/Memory.cpp b/src/Memory.cpp index 59922be3..73476477 100644 --- a/src/Memory.cpp +++ b/src/Memory.cpp @@ -8,6 +8,7 @@ */ +#include "Init.h" #include "Memory.h" @@ -19,6 +20,8 @@ Memory::Memory() Memory::~Memory() { + CloseDebugFiles(); + FreeMemory(); } @@ -30,6 +33,7 @@ void Memory::Reset() void Memory::ResetThread(const unsigned thrId) { + if(memory[thrId] == nullptr || memory[thrId]->transTable == nullptr) return; memory[thrId]->transTable->ResetMemory(TT_RESET_FREE_MEMORY); memory[thrId]->memUsed = Memory::MemoryInUseMB(thrId); } @@ -40,6 +44,8 @@ void Memory::ReturnThread(const unsigned thrId) if(memory[thrId] == nullptr || memory[thrId]->transTable == nullptr) return; memory[thrId]->transTable->ReturnAllMemory(); memory[thrId]->memUsed = Memory::MemoryInUseMB(thrId); + delete memory[thrId]->transTable; + delete memory[thrId]; } @@ -104,7 +110,7 @@ ThreadData * Memory::GetPtr(const unsigned thrId) { if (thrId >= nThreads) { - cout << "Memory::GetPtr: " << thrId << " vs. " << nThreads << endl; + cout << "Memory::GetPtr: " << thrId << " vs. " << nThreads << " vs. " << memory.size() << endl; exit(1); } return memory[thrId]; diff --git a/src/dds.cpp b/src/dds.cpp index fad30225..0048df95 100644 --- a/src/dds.cpp +++ b/src/dds.cpp @@ -72,8 +72,6 @@ static void __attribute__ ((constructor)) libInit(void) static void __attribute__ ((destructor)) libEnd(void) { - CloseDebugFiles(); - FreeMemory(); } #endif