• R/O
  • SSH
  • HTTPS

chibios: Commit


Commit MetaInfo

Revision15357 (tree)
Time2022-01-14 21:51:03
Authorgdisirio

Log Message

Loading and passing arguments/variables working now.

Change Summary

Incremental Difference

--- trunk/demos/STM32/RT-STM32L4R9-DISCOVERY-RAM_SB_HOST_DYNAMIC/main.c (revision 15356)
+++ trunk/demos/STM32/RT-STM32L4R9-DISCOVERY-RAM_SB_HOST_DYNAMIC/main.c (revision 15357)
@@ -97,6 +97,16 @@
9797 /* Sandbox objects.*/
9898 sb_class_t sbx1;
9999
100+static const char *sbx1_argv[] = {
101+ "sbx1",
102+ NULL
103+};
104+
105+static const char *sbx1_envp[] = {
106+ "PATH=/bin",
107+ NULL
108+};
109+
100110 static THD_WORKING_AREA(waUnprivileged1, 1024);
101111
102112 /*===========================================================================*/
@@ -300,7 +310,7 @@
300310 /* Starting sandboxed thread 1.*/
301311 if (sbStartThread(&sbx1, "sbx1",
302312 waUnprivileged1, sizeof (waUnprivileged1), NORMALPRIO - 1,
303- 0, NULL, NULL) == NULL) {
313+ sbx1_argv, sbx1_envp) == NULL) {
304314 chSysHalt("sbx1 failed");
305315 }
306316 }
--- trunk/os/oslib/include/chmemchecks.h (revision 15356)
+++ trunk/os/oslib/include/chmemchecks.h (revision 15357)
@@ -73,7 +73,6 @@
7373 extern const memory_area_t __ch_mem_writable_areas[];
7474 extern const memory_area_t __ch_mem_readable_areas[];
7575 extern const memory_area_t __ch_mem_executable_areas[];
76-
7776 #endif
7877
7978 #ifdef __cplusplus
--- trunk/os/sb/apps/msh/Makefile (revision 15356)
+++ trunk/os/sb/apps/msh/Makefile (revision 15357)
@@ -108,7 +108,7 @@
108108 # RTOS files (optional).
109109 #include $(CHIBIOS)/os/rt/rt.mk
110110 #include $(CHIBIOS)/os/common/ports/ARMv7-M/compilers/GCC/mk/port.mk
111-#include $(CHIBIOS)/os/sb/user/sbuser.mk
111+include $(CHIBIOS)/os/sb/user/sbuser.mk
112112 # Auto-build files in ./source recursively.
113113 include $(CHIBIOS)/tools/mk/autobuild.mk
114114 # Other files (optional).
--- trunk/os/sb/apps/msh/main.c (revision 15356)
+++ trunk/os/sb/apps/msh/main.c (revision 15357)
@@ -17,18 +17,29 @@
1717 #include <stdio.h>
1818 #include <stdbool.h>
1919
20-//#include "sbuser.h"
20+#include "sbuser.h"
2121
22-//const int dogsandcats = 0x55;
23-
2422 /*
2523 * Application entry point.
2624 */
27-int main(void) {
28-// unsigned i = 1U;
25+int main(int argc, char *argv[], char *envp[]) {
26+ char *s;
27+ unsigned i = 1U;
2928
29+ printf("argc: %d\r\n", argc);
30+ printf("argv: ");
31+ while ((s = *argv++) != NULL) {
32+ printf("%s", s);
33+ }
34+ printf("\r\n");
35+ printf("envp: ");
36+ while ((s = *envp++) != NULL) {
37+ printf("%s", s);
38+ }
39+ printf("\r\n");
40+
3041 while (true) {
31-// printf("#1 Hello World (%u)!!\r\n", i++);
32-// sbSleepMilliseconds(500);
42+ printf("#1 Hello World (%u)!!\r\n", i++);
43+ sbSleepMilliseconds(500);
3344 }
3445 }
--- trunk/os/sb/host/sbelf.c (revision 15356)
+++ trunk/os/sb/host/sbelf.c (revision 15357)
@@ -58,6 +58,7 @@
5858 #define R_ARM_ABS32 2U
5959 #define R_ARM_THM_PC22 10U
6060 #define R_ARM_THM_JUMP24 30U
61+#define R_ARM_PREL31 42U
6162 #define R_ARM_THM_MOVW_ABS_NC 47U
6263 #define R_ARM_THM_MOVT_ABS 48U
6364
@@ -304,7 +305,7 @@
304305 uint32_t relocation_address, offset;
305306
306307 /* Relocation point address.*/
307- relocation_address = (uint32_t)esip->area.base + rp->r_offset;
308+ relocation_address = (uint32_t)ctxp->map->base + rp->r_offset;
308309 if (!chMemIsSpaceWithinX(&esip->area,
309310 (const void *)relocation_address,
310311 sizeof (uint32_t))) {
@@ -351,6 +352,7 @@
351352 break;
352353 case R_ARM_THM_PC22:
353354 case R_ARM_THM_JUMP24:
355+ case R_ARM_PREL31:
354356 /* To be ignored.*/
355357 break;
356358 default:
--- trunk/os/sb/host/sbhost.c (revision 15356)
+++ trunk/os/sb/host/sbhost.c (revision 15357)
@@ -32,6 +32,15 @@
3232 /* Module local definitions. */
3333 /*===========================================================================*/
3434
35+#define PUSHSPACE(sp, n) do { \
36+ (sp) = (void *)((uint8_t *)(sp) - (n)); \
37+} while (false)
38+
39+#define PUSHTYPE(type, sp, p) do { \
40+ PUSHSPACE(sp, sizeof (type)); \
41+ *(type *)(void *)(sp) = (type)(p); \
42+} while (false)
43+
3544 /*===========================================================================*/
3645 /* Module exported variables. */
3746 /*===========================================================================*/
@@ -57,6 +66,45 @@
5766 /* Module exported functions. */
5867 /*===========================================================================*/
5968
69+size_t sb_strv_getsize(const char *v[], int *np) {
70+ const char* s;
71+ size_t size;
72+ int n;
73+
74+ size = sizeof (const char *);
75+ if (v != NULL) {
76+ n = 0;
77+ while ((s = *v) != NULL) {
78+ size += sizeof (const char *) + strlen(s) + (size_t)1;
79+ n++;
80+ v++;
81+ }
82+
83+ if (np != NULL) {
84+ *np = n;
85+ }
86+ }
87+
88+ return MEM_ALIGN_NEXT(size, MEM_NATURAL_ALIGN);
89+}
90+
91+void sb_strv_copy(const char *sp[], void *dp, int n) {
92+ char **vp;
93+ char *cp;
94+ int i;
95+
96+ vp = (char **)dp;
97+ cp = (char *)dp + ((n + 1) * sizeof (char *));
98+ for (i = 0; i < n; i++) {
99+ const char *ss = sp[i];
100+ *(vp + i) = cp;
101+ while ((*cp++ = *ss++) != '\0') {
102+ /* Copy.*/
103+ }
104+ }
105+ *(vp + n) = NULL;
106+}
107+
60108 bool sb_is_valid_read_range(sb_class_t *sbcp, const void *start, size_t size) {
61109 const sb_memory_region_t *rp = &sbcp->config->regions[0];
62110
@@ -164,7 +212,6 @@
164212 * @param[out] wsp pointer to a working area dedicated to the thread stack
165213 * @param[in] size size of the working area
166214 * @param[in] prio the priority level for the new thread
167- * @param[in] argc number of parameters for the sandbox
168215 * @param[in] argv array of parameters for the sandbox
169216 * @param[in] envp array of environment variables for the sandbox
170217 * @return The thread pointer.
@@ -172,11 +219,13 @@
172219 */
173220 thread_t *sbStartThread(sb_class_t *sbcp, const char *name,
174221 void *wsp, size_t size, tprio_t prio,
175- int argc, char *argv[], char *envp[]) {
222+ const char *argv[], const char *envp[]) {
176223 thread_t *utp;
177224 const sb_header_t *sbhp;
178225 const sb_config_t *config = sbcp->config;
179- uint32_t *sp;
226+ void *usp, *uargv, *uenvp;
227+ size_t envsize, argsize, parsize;
228+ int uargc, uenvc;
180229
181230 /* Header location.*/
182231 sbhp = (const sb_header_t *)(void *)config->regions[config->code_region].area.base;
@@ -199,13 +248,41 @@
199248 }
200249
201250 /* Setting up an initial stack for the sandbox.*/
202- sp = (uint32_t *)(void *)(config->regions[config->data_region].area.base +
203- config->regions[config->data_region].area.size);
204- sp -= 3 * sizeof (uint32_t);
205- sp[0] = (uint32_t)argc;
206- sp[1] = (uint32_t)argv;
207- sp[2] = (uint32_t)envp;
251+ usp = (config->regions[config->data_region].area.base +
252+ config->regions[config->data_region].area.size);
208253
254+ /* Allocating space for environment variables.*/
255+ envsize = sb_strv_getsize(envp, &uenvc);
256+ PUSHSPACE(usp, envsize);
257+ uenvp = usp;
258+
259+ /* Allocating space for arguments.*/
260+ argsize = sb_strv_getsize(argv, &uargc);
261+ PUSHSPACE(usp, argsize);
262+ uargv = usp;
263+
264+ /* Allocating space for parameters.*/
265+ if (MEM_IS_ALIGNED(usp, PORT_STACK_ALIGN)) {
266+ parsize = sizeof (int) + sizeof (const char **) + sizeof (const char **) + sizeof (int);
267+ }
268+ else {
269+ parsize = sizeof (const char **) + sizeof (const char **) + sizeof (int);
270+ }
271+ PUSHSPACE(usp, parsize);
272+
273+ /* Checking stack allocation.*/
274+ if (!chMemIsSpaceWithinX(&config->regions[config->data_region].area,
275+ usp, envsize + argsize + parsize)) {
276+ return NULL;
277+ }
278+
279+ /* Initializing stack.*/
280+ sb_strv_copy(envp, uenvp, uenvc);
281+ sb_strv_copy(argv, uargv, uargc);
282+ *((uint32_t *)usp + 2) = (uint32_t)uenvp;
283+ *((uint32_t *)usp + 1) = (uint32_t)uargv;
284+ *((uint32_t *)usp + 0) = (uint32_t)uargc;
285+
209286 unprivileged_thread_descriptor_t utd = {
210287 .name = name,
211288 .wbase = (stkalign_t *)wsp,
@@ -212,7 +289,7 @@
212289 .wend = (stkalign_t *)wsp + (size / sizeof (stkalign_t)),
213290 .prio = prio,
214291 .u_pc = sbhp->hdr_entry,
215- .u_psp = (uint32_t)sp,
292+ .u_psp = (uint32_t)usp,
216293 .arg = (void *)sbcp
217294 };
218295 #if PORT_SWITCHED_REGIONS_NUMBER > 0
--- trunk/os/sb/host/sbhost.h (revision 15356)
+++ trunk/os/sb/host/sbhost.h (revision 15357)
@@ -92,6 +92,7 @@
9292 extern "C" {
9393 #endif
9494 void port_syscall(struct port_extctx *ctxp, uint32_t n);
95+ size_t sb_strv_getsize(const char *v[], int *np);
9596 bool sb_is_valid_read_range(sb_class_t *sbcp, const void *start, size_t size);
9697 bool sb_is_valid_write_range(sb_class_t *sbcp, void *start, size_t size);
9798 size_t sb_check_string(sb_class_t *sbcp, const char *s, size_t max);
@@ -100,7 +101,7 @@
100101 void sbObjectInit(sb_class_t *sbcp, const sb_config_t *config);
101102 thread_t *sbStartThread(sb_class_t *sbcp, const char *name,
102103 void *wsp, size_t size, tprio_t prio,
103- int argc, char *argv[], char *envp[]);
104+ const char *argv[], const char *envp[]);
104105 bool sbIsThreadRunningX(sb_class_t *sbcp);
105106 #if CH_CFG_USE_WAITEXIT == TRUE
106107 msg_t sbWaitThread(sb_class_t *sbcp);
Show on old repository browser