You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

357 lines
16 KiB

  1. From e8971dd4914c9d42938c4c885b4ac6d784d7e0ff Mon Sep 17 00:00:00 2001
  2. From: Hauke Mehrtens <hauke@hauke-m.de>
  3. Date: Mon, 3 Oct 2016 23:22:36 +0200
  4. Subject: [PATCH 2/3] csdk: move OCClientResponse from stack to heap
  5. OCClientResponse is about 50KByte and should not be stored on the
  6. stack. On LEDE with MIPS, musl libc this causes a segmentation fault.
  7. Moving this structure to the heap is the simple solution for this
  8. problem, but this structure should be shrined.
  9. There are probably more places were this is stored on the stack and
  10. will cause problems. This fixes the other issue I saw in
  11. https://jira.iotivity.org/browse/IOT-1374
  12. Change-Id: I45d8aee4a8151fea51d3318acb1eea61ce579060
  13. Signed-off-by: Hauke Mehrtens <hauke@hauke-m.de>
  14. ---
  15. resource/csdk/stack/src/ocstack.c | 163 ++++++++++++++++++++++++--------------
  16. 1 file changed, 102 insertions(+), 61 deletions(-)
  17. --- a/resource/csdk/stack/src/ocstack.c
  18. +++ b/resource/csdk/stack/src/ocstack.c
  19. @@ -1042,7 +1042,7 @@ OCStackResult HandlePresenceResponse(con
  20. OCStackApplicationResult cbResult = OC_STACK_DELETE_TRANSACTION;
  21. ClientCB * cbNode = NULL;
  22. char *resourceTypeName = NULL;
  23. - OCClientResponse response = {.devAddr = {.adapter = OC_DEFAULT_ADAPTER}};
  24. + OCClientResponse *response = NULL;
  25. OCStackResult result = OC_STACK_ERROR;
  26. uint32_t maxAge = 0;
  27. int uriLen;
  28. @@ -1057,15 +1057,23 @@ OCStackResult HandlePresenceResponse(con
  29. return OC_STACK_ERROR;
  30. }
  31. - response.payload = NULL;
  32. - response.result = OC_STACK_OK;
  33. + response = (OCClientResponse *)OICCalloc(1, sizeof(*response));
  34. + if (!response)
  35. + {
  36. + OIC_LOG(ERROR, TAG, "Allocating memory for response failed");
  37. + return OC_STACK_ERROR;
  38. + }
  39. + response->devAddr.adapter = OC_DEFAULT_ADAPTER;
  40. - CopyEndpointToDevAddr(endpoint, &response.devAddr);
  41. - FixUpClientResponse(&response);
  42. + response->payload = NULL;
  43. + response->result = OC_STACK_OK;
  44. +
  45. + CopyEndpointToDevAddr(endpoint, &response->devAddr);
  46. + FixUpClientResponse(response);
  47. if (responseInfo->info.payload)
  48. {
  49. - result = OCParsePayload(&response.payload,
  50. + result = OCParsePayload(&response->payload,
  51. PAYLOAD_TYPE_PRESENCE,
  52. responseInfo->info.payload,
  53. responseInfo->info.payloadSize);
  54. @@ -1075,15 +1083,15 @@ OCStackResult HandlePresenceResponse(con
  55. OIC_LOG(ERROR, TAG, "Presence parse failed");
  56. goto exit;
  57. }
  58. - if(!response.payload || response.payload->type != PAYLOAD_TYPE_PRESENCE)
  59. + if(!response->payload || response->payload->type != PAYLOAD_TYPE_PRESENCE)
  60. {
  61. OIC_LOG(ERROR, TAG, "Presence payload was wrong type");
  62. result = OC_STACK_ERROR;
  63. goto exit;
  64. }
  65. - response.sequenceNumber = ((OCPresencePayload*)response.payload)->sequenceNumber;
  66. - resourceTypeName = ((OCPresencePayload*)response.payload)->resourceType;
  67. - maxAge = ((OCPresencePayload*)response.payload)->maxAge;
  68. + response->sequenceNumber = ((OCPresencePayload*)response->payload)->sequenceNumber;
  69. + resourceTypeName = ((OCPresencePayload*)response->payload)->resourceType;
  70. + maxAge = ((OCPresencePayload*)response->payload)->maxAge;
  71. }
  72. // check for unicast presence
  73. @@ -1091,6 +1099,7 @@ OCStackResult HandlePresenceResponse(con
  74. responseInfo->isMulticast);
  75. if (uriLen < 0 || (size_t)uriLen >= sizeof (presenceUri))
  76. {
  77. + OICFree(response);
  78. return OC_STACK_INVALID_URI;
  79. }
  80. OIC_LOG(ERROR, TAG, "check for unicast presence");
  81. @@ -1118,7 +1127,7 @@ OCStackResult HandlePresenceResponse(con
  82. if (presenceSubscribe)
  83. {
  84. - if(cbNode->sequenceNumber == response.sequenceNumber)
  85. + if(cbNode->sequenceNumber == response->sequenceNumber)
  86. {
  87. OIC_LOG(INFO, TAG, "No presence change");
  88. ResetPresenceTTL(cbNode, maxAge);
  89. @@ -1129,7 +1138,7 @@ OCStackResult HandlePresenceResponse(con
  90. if(maxAge == 0)
  91. {
  92. OIC_LOG(INFO, TAG, "Stopping presence");
  93. - response.result = OC_STACK_PRESENCE_STOPPED;
  94. + response->result = OC_STACK_PRESENCE_STOPPED;
  95. if(cbNode->presence)
  96. {
  97. OICFree(cbNode->presence->timeOut);
  98. @@ -1165,7 +1174,7 @@ OCStackResult HandlePresenceResponse(con
  99. ResetPresenceTTL(cbNode, maxAge);
  100. - cbNode->sequenceNumber = response.sequenceNumber;
  101. + cbNode->sequenceNumber = response->sequenceNumber;
  102. }
  103. }
  104. else
  105. @@ -1175,7 +1184,7 @@ OCStackResult HandlePresenceResponse(con
  106. if (0 == maxAge)
  107. {
  108. OIC_LOG(INFO, TAG, "Stopping presence");
  109. - response.result = OC_STACK_PRESENCE_STOPPED;
  110. + response->result = OC_STACK_PRESENCE_STOPPED;
  111. }
  112. }
  113. @@ -1191,7 +1200,7 @@ OCStackResult HandlePresenceResponse(con
  114. OIC_LOG(INFO, TAG, "Callback for presence");
  115. - cbResult = cbNode->callBack(cbNode->context, cbNode->handle, &response);
  116. + cbResult = cbNode->callBack(cbNode->context, cbNode->handle, response);
  117. if (cbResult == OC_STACK_DELETE_TRANSACTION)
  118. {
  119. @@ -1199,7 +1208,8 @@ OCStackResult HandlePresenceResponse(con
  120. }
  121. exit:
  122. - OCPayloadDestroy(response.payload);
  123. + OCPayloadDestroy(response->payload);
  124. + OICFree(response);
  125. return result;
  126. }
  127. @@ -1240,36 +1250,53 @@ void OCHandleResponse(const CAEndpoint_t
  128. OIC_LOG(INFO, TAG, "Receiving A Timeout for this token");
  129. OIC_LOG(INFO, TAG, "Calling into application address space");
  130. - OCClientResponse response =
  131. - {.devAddr = {.adapter = OC_DEFAULT_ADAPTER}};
  132. - CopyEndpointToDevAddr(endPoint, &response.devAddr);
  133. - FixUpClientResponse(&response);
  134. - response.resourceUri = responseInfo->info.resourceUri;
  135. - memcpy(response.identity.id, responseInfo->info.identity.id,
  136. - sizeof (response.identity.id));
  137. - response.identity.id_length = responseInfo->info.identity.id_length;
  138. + OCClientResponse *response = NULL;
  139. +
  140. + response = (OCClientResponse *)OICCalloc(1, sizeof(*response));
  141. + if (!response)
  142. + {
  143. + OIC_LOG(ERROR, TAG, "Allocating memory for response failed");
  144. + return;
  145. + }
  146. +
  147. + response->devAddr.adapter = OC_DEFAULT_ADAPTER;
  148. + CopyEndpointToDevAddr(endPoint, &response->devAddr);
  149. + FixUpClientResponse(response);
  150. + response->resourceUri = responseInfo->info.resourceUri;
  151. + memcpy(response->identity.id, responseInfo->info.identity.id,
  152. + sizeof (response->identity.id));
  153. + response->identity.id_length = responseInfo->info.identity.id_length;
  154. - response.result = CAResponseToOCStackResult(responseInfo->result);
  155. + response->result = CAResponseToOCStackResult(responseInfo->result);
  156. cbNode->callBack(cbNode->context,
  157. - cbNode->handle, &response);
  158. + cbNode->handle, response);
  159. FindAndDeleteClientCB(cbNode);
  160. + OICFree(response);
  161. }
  162. else
  163. {
  164. OIC_LOG(INFO, TAG, "This is a regular response, A client call back is found");
  165. OIC_LOG(INFO, TAG, "Calling into application address space");
  166. - OCClientResponse response =
  167. - {.devAddr = {.adapter = OC_DEFAULT_ADAPTER}};
  168. - response.sequenceNumber = MAX_SEQUENCE_NUMBER + 1;
  169. - CopyEndpointToDevAddr(endPoint, &response.devAddr);
  170. - FixUpClientResponse(&response);
  171. - response.resourceUri = responseInfo->info.resourceUri;
  172. - memcpy(response.identity.id, responseInfo->info.identity.id,
  173. - sizeof (response.identity.id));
  174. - response.identity.id_length = responseInfo->info.identity.id_length;
  175. + OCClientResponse *response = NULL;
  176. - response.result = CAResponseToOCStackResult(responseInfo->result);
  177. + response = (OCClientResponse *)OICCalloc(1, sizeof(*response));
  178. + if (!response)
  179. + {
  180. + OIC_LOG(ERROR, TAG, "Allocating memory for response failed");
  181. + return;
  182. + }
  183. +
  184. + response->devAddr.adapter = OC_DEFAULT_ADAPTER;
  185. + response->sequenceNumber = MAX_SEQUENCE_NUMBER + 1;
  186. + CopyEndpointToDevAddr(endPoint, &response->devAddr);
  187. + FixUpClientResponse(response);
  188. + response->resourceUri = responseInfo->info.resourceUri;
  189. + memcpy(response->identity.id, responseInfo->info.identity.id,
  190. + sizeof (response->identity.id));
  191. + response->identity.id_length = responseInfo->info.identity.id_length;
  192. +
  193. + response->result = CAResponseToOCStackResult(responseInfo->result);
  194. if(responseInfo->info.payload &&
  195. responseInfo->info.payloadSize)
  196. @@ -1359,21 +1386,23 @@ void OCHandleResponse(const CAEndpoint_t
  197. {
  198. OIC_LOG_V(ERROR, TAG, "Unknown Payload type: %d %s",
  199. cbNode->method, cbNode->requestUri);
  200. + OICFree(response);
  201. return;
  202. }
  203. - if(OC_STACK_OK != OCParsePayload(&response.payload,
  204. + if(OC_STACK_OK != OCParsePayload(&response->payload,
  205. type,
  206. responseInfo->info.payload,
  207. responseInfo->info.payloadSize))
  208. {
  209. OIC_LOG(ERROR, TAG, "Error converting payload");
  210. - OCPayloadDestroy(response.payload);
  211. + OCPayloadDestroy(response->payload);
  212. + OICFree(response);
  213. return;
  214. }
  215. }
  216. - response.numRcvdVendorSpecificHeaderOptions = 0;
  217. + response->numRcvdVendorSpecificHeaderOptions = 0;
  218. if(responseInfo->info.numOptions > 0)
  219. {
  220. int start = 0;
  221. @@ -1391,19 +1420,20 @@ void OCHandleResponse(const CAEndpoint_t
  222. observationOption =
  223. (observationOption << 8) | optionData[i];
  224. }
  225. - response.sequenceNumber = observationOption;
  226. - response.numRcvdVendorSpecificHeaderOptions = responseInfo->info.numOptions - 1;
  227. + response->sequenceNumber = observationOption;
  228. + response->numRcvdVendorSpecificHeaderOptions = responseInfo->info.numOptions - 1;
  229. start = 1;
  230. }
  231. else
  232. {
  233. - response.numRcvdVendorSpecificHeaderOptions = responseInfo->info.numOptions;
  234. + response->numRcvdVendorSpecificHeaderOptions = responseInfo->info.numOptions;
  235. }
  236. - if(response.numRcvdVendorSpecificHeaderOptions > MAX_HEADER_OPTIONS)
  237. + if(response->numRcvdVendorSpecificHeaderOptions > MAX_HEADER_OPTIONS)
  238. {
  239. OIC_LOG(ERROR, TAG, "#header options are more than MAX_HEADER_OPTIONS");
  240. - OCPayloadDestroy(response.payload);
  241. + OCPayloadDestroy(response->payload);
  242. + OICFree(response);
  243. return;
  244. }
  245. @@ -1411,19 +1441,19 @@ void OCHandleResponse(const CAEndpoint_t
  246. {
  247. if(&(responseInfo->info.options[i]))
  248. {
  249. - memcpy (&(response.rcvdVendorSpecificHeaderOptions[i-start]),
  250. + memcpy (&(response->rcvdVendorSpecificHeaderOptions[i-start]),
  251. &(responseInfo->info.options[i]), sizeof(OCHeaderOption));
  252. }
  253. }
  254. }
  255. if (cbNode->method == OC_REST_OBSERVE &&
  256. - response.sequenceNumber > OC_OFFSET_SEQUENCE_NUMBER &&
  257. + response->sequenceNumber > OC_OFFSET_SEQUENCE_NUMBER &&
  258. cbNode->sequenceNumber <= MAX_SEQUENCE_NUMBER &&
  259. - response.sequenceNumber <= cbNode->sequenceNumber)
  260. + response->sequenceNumber <= cbNode->sequenceNumber)
  261. {
  262. OIC_LOG_V(INFO, TAG, "Received stale notification. Number :%d",
  263. - response.sequenceNumber);
  264. + response->sequenceNumber);
  265. }
  266. else
  267. {
  268. @@ -1432,13 +1462,13 @@ void OCHandleResponse(const CAEndpoint_t
  269. char *targetUri = strstr(cbNode->requestUri, OC_RSRVD_RD_URI);
  270. if (targetUri)
  271. {
  272. - OCUpdateResourceInsWithResponse(cbNode->requestUri, &response);
  273. + OCUpdateResourceInsWithResponse(cbNode->requestUri, response);
  274. }
  275. #endif
  276. OCStackApplicationResult appFeedback = cbNode->callBack(cbNode->context,
  277. cbNode->handle,
  278. - &response);
  279. - cbNode->sequenceNumber = response.sequenceNumber;
  280. + response);
  281. + cbNode->sequenceNumber = response->sequenceNumber;
  282. if (appFeedback == OC_STACK_DELETE_TRANSACTION)
  283. {
  284. @@ -1459,7 +1489,8 @@ void OCHandleResponse(const CAEndpoint_t
  285. CA_MSG_ACKNOWLEDGE, 0, NULL, NULL, 0, NULL, CA_RESPONSE_FOR_RES);
  286. }
  287. - OCPayloadDestroy(response.payload);
  288. + OCPayloadDestroy(response->payload);
  289. + OICFree(response);
  290. }
  291. return;
  292. }
  293. @@ -1585,16 +1616,26 @@ void HandleCAErrorResponse(const CAEndpo
  294. errorInfo->info.tokenLength, NULL, NULL);
  295. if (cbNode)
  296. {
  297. - OCClientResponse response = { .devAddr = { .adapter = OC_DEFAULT_ADAPTER } };
  298. - CopyEndpointToDevAddr(endPoint, &response.devAddr);
  299. - FixUpClientResponse(&response);
  300. - response.resourceUri = errorInfo->info.resourceUri;
  301. - memcpy(response.identity.id, errorInfo->info.identity.id,
  302. - sizeof (response.identity.id));
  303. - response.identity.id_length = errorInfo->info.identity.id_length;
  304. - response.result = CAResultToOCResult(errorInfo->result);
  305. + OCClientResponse *response = NULL;
  306. +
  307. + response = (OCClientResponse *)OICCalloc(1, sizeof(*response));
  308. + if (!response)
  309. + {
  310. + OIC_LOG(ERROR, TAG, "Allocating memory for response failed");
  311. + return;
  312. + }
  313. +
  314. + response->devAddr.adapter = OC_DEFAULT_ADAPTER;
  315. + CopyEndpointToDevAddr(endPoint, &response->devAddr);
  316. + FixUpClientResponse(response);
  317. + response->resourceUri = errorInfo->info.resourceUri;
  318. + memcpy(response->identity.id, errorInfo->info.identity.id,
  319. + sizeof (response->identity.id));
  320. + response->identity.id_length = errorInfo->info.identity.id_length;
  321. + response->result = CAResultToOCResult(errorInfo->result);
  322. - cbNode->callBack(cbNode->context, cbNode->handle, &response);
  323. + cbNode->callBack(cbNode->context, cbNode->handle, response);
  324. + OICFree(response);
  325. }
  326. ResourceObserver *observer = GetObserverUsingToken(errorInfo->info.token,