xref: /haiku/src/system/libroot/os/image.cpp (revision 1e60bdeab63fa7a57bc9a55b032052e95a18bd2c)
1 /*
2  * Copyright 2003-2007, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 #include <image.h>
7 #include <image_private.h>
8 
9 #include <stdlib.h>
10 #include <string.h>
11 
12 #include <algorithm>
13 #include <new>
14 
15 #include <fs_attr.h>
16 
17 #include <AutoDeleter.h>
18 
19 #include <libroot_private.h>
20 #include <runtime_loader.h>
21 #include <syscalls.h>
22 #include <syscall_load_image.h>
23 #include <user_runtime.h>
24 
25 
26 struct EnvironmentFilter {
27 	EnvironmentFilter()
28 		:
29 		fBuffer(NULL),
30 		fEntries(NULL),
31 		fBufferSize(0),
32 		fEntryCount(0),
33 		fAdditionalEnvCount(0),
34 		fNextEntryIndex(0)
35 	{
36 	}
37 
38 	~EnvironmentFilter()
39 	{
40 		free(fBuffer);
41 		delete[] fEntries;
42 	}
43 
44 	void Init(const char* path, const char* const* env, size_t envCount)
45 	{
46 		int fd = open(path, O_RDONLY);
47 		if (fd < 0)
48 			return;
49 		FileDescriptorCloser fdCloser(fd);
50 
51 		static const char* const kEnvAttribute = "SYS:ENV";
52 		attr_info info;
53 		if (fs_stat_attr(fd, kEnvAttribute, &info) < 0)
54 			return;
55 
56 		_Init(fd, kEnvAttribute, info.size, env, envCount);
57 	}
58 
59 	size_t AdditionalSlotsNeeded() const
60 	{
61 		return fAdditionalEnvCount;
62 	}
63 
64 	size_t AdditionalSizeNeeded() const
65 	{
66 		return fBufferSize + fAdditionalEnvCount * sizeof(char*);
67 	}
68 
69 	size_t PrepareSlot(const char* env, int32 index, char* buffer)
70 	{
71 		if (fNextEntryIndex < fEntryCount
72 			&& fEntries[fNextEntryIndex].index == index) {
73 			env = fEntries[fNextEntryIndex].replacement;
74 			fNextEntryIndex++;
75 		}
76 
77 		return _FillSlot(env, buffer);
78 	}
79 
80 	void PrepareAdditionalSlots(char**& slot, char*& buffer)
81 	{
82 		for (size_t i = 0; i < fAdditionalEnvCount; i++) {
83 			size_t envSize = _FillSlot(fEntries[i].replacement, buffer);
84 			*slot++ = buffer;
85 			buffer += envSize;
86 		}
87 	}
88 
89 private:
90 	void _Init(int fd, const char* attribute, size_t size,
91 		const char* const* env, size_t envCount)
92 	{
93 		if (size == 0)
94 			return;
95 
96 		// read the attribute
97 		char* buffer = (char*)malloc(size + 1);
98 		if (buffer == NULL)
99 			return;
100 		MemoryDeleter bufferDeleter(buffer);
101 
102 		ssize_t bytesRead = fs_read_attr(fd, attribute, B_STRING_TYPE, 0,
103 			buffer, size);
104 		if (bytesRead < 0 || (size_t)bytesRead != size)
105 			return;
106 		buffer[size] = '\0';
107 
108 		// deescape the buffer and count the entries
109 		size_t entryCount = 1;
110 		char* out = buffer;
111 		for (const char* c = buffer; *c != '\0'; c++) {
112 			if (*c == '\\') {
113 				c++;
114 				if (*c == '\0')
115 					break;
116 				if (*c == '0') {
117 					*out++ = '\0';
118 					entryCount++;
119 				} else
120 					*out++ = *c;
121 			} else
122 				*out++ = *c;
123 		}
124 		*out++ = '\0';
125 		size = out - buffer + 1;
126 
127 		// create an entry array
128 		fEntries = new(std::nothrow) Entry[entryCount];
129 		if (fEntries == NULL)
130 			return;
131 
132 		bufferDeleter.Detach();
133 		fBuffer = buffer;
134 		fBufferSize = size;
135 
136 		// init the entries
137 		out = buffer;
138 		for (size_t i = 0; i < entryCount; i++) {
139 			const char* separator = strchr(out, '=');
140 			if (separator != NULL && separator != out) {
141 				fEntries[fEntryCount].replacement = out;
142 				fEntries[fEntryCount].index = _FindEnvEntry(env, envCount, out,
143 					separator - out);
144 				if (fEntries[fEntryCount].index < 0)
145 					fAdditionalEnvCount++;
146 				fEntryCount++;
147 			}
148 			out += strlen(out) + 1;
149 		}
150 
151 		if (fEntryCount > 1)
152 			std::sort(fEntries, fEntries + fEntryCount);
153 
154 		// Advance fNextEntryIndex to the first entry pointing to an existing
155 		// env variable.
156 		while (fNextEntryIndex < fEntryCount
157 			&& fEntries[fNextEntryIndex].index < 0) {
158 			fNextEntryIndex++;
159 		}
160 	}
161 
162 	int32 _FindEnvEntry(const char* const* env, size_t envCount,
163 		const char* variable, size_t variableLength)
164 	{
165 		for (size_t i = 0; i < envCount; i++) {
166 			if (strncmp(env[i], variable, variableLength) == 0
167 				&& env[i][variableLength] == '=') {
168 				return i;
169 			}
170 		}
171 
172 		return -1;
173 	}
174 
175 	size_t _FillSlot(const char* env, char* buffer)
176 	{
177 		size_t envSize = strlen(env) + 1;
178 		memcpy(buffer, env, envSize);
179 		return envSize;
180 	}
181 
182 private:
183 	struct Entry {
184 		char*	replacement;
185 		int32	index;
186 
187 		bool operator<(const Entry& other) const
188 		{
189 			return index < other.index;
190 		}
191 	};
192 
193 private:
194 	char*	fBuffer;
195 	Entry*	fEntries;
196 	size_t	fBufferSize;
197 	size_t	fEntryCount;
198 	size_t	fAdditionalEnvCount;
199 	size_t	fNextEntryIndex;
200 };
201 
202 
203 thread_id
204 load_image(int32 argCount, const char **args, const char **environ)
205 {
206 	char invoker[B_FILE_NAME_LENGTH];
207 	char **newArgs = NULL;
208 	int32 envCount = 0;
209 	thread_id thread;
210 
211 	if (argCount < 1 || environ == NULL)
212 		return B_BAD_VALUE;
213 
214 	// test validity of executable + support for scripts
215 	{
216 		status_t status = __test_executable(args[0], invoker);
217 		if (status < B_OK)
218 			return status;
219 
220 		if (invoker[0]) {
221 			status = __parse_invoke_line(invoker, &newArgs,
222 				(char * const **)&args, &argCount, args[0]);
223 			if (status < B_OK)
224 				return status;
225 		}
226 	}
227 
228 	// count environment variables
229 	while (environ[envCount] != NULL)
230 		envCount++;
231 
232 	char** flatArgs = NULL;
233 	size_t flatArgsSize;
234 	status_t status = __flatten_process_args(args, argCount, environ,
235 		&envCount, args[0], &flatArgs, &flatArgsSize);
236 
237 	if (status == B_OK) {
238 		thread = _kern_load_image(flatArgs, flatArgsSize, argCount, envCount,
239 			B_NORMAL_PRIORITY, B_WAIT_TILL_LOADED, -1, 0);
240 
241 		free(flatArgs);
242 	} else
243 		thread = status;
244 
245 	free(newArgs);
246 	return thread;
247 }
248 
249 
250 image_id
251 load_add_on(char const *name)
252 {
253 	if (name == NULL)
254 		return B_BAD_VALUE;
255 
256 	return __gRuntimeLoader->load_add_on(name, 0);
257 }
258 
259 
260 status_t
261 unload_add_on(image_id id)
262 {
263 	return __gRuntimeLoader->unload_add_on(id);
264 }
265 
266 
267 status_t
268 get_image_symbol(image_id id, char const *symbolName, int32 symbolType,
269 	void **_location)
270 {
271 	return __gRuntimeLoader->get_image_symbol(id, symbolName, symbolType,
272 		false, NULL, _location);
273 }
274 
275 
276 status_t
277 get_image_symbol_etc(image_id id, char const *symbolName, int32 symbolType,
278 	bool recursive, image_id *_inImage, void **_location)
279 {
280 	return __gRuntimeLoader->get_image_symbol(id, symbolName, symbolType,
281 		recursive, _inImage, _location);
282 }
283 
284 
285 status_t
286 get_nth_image_symbol(image_id id, int32 num, char *nameBuffer, int32 *_nameLength,
287 	int32 *_symbolType, void **_location)
288 {
289 	return __gRuntimeLoader->get_nth_image_symbol(id, num, nameBuffer, _nameLength, _symbolType, _location);
290 }
291 
292 
293 status_t
294 _get_image_info(image_id id, image_info *info, size_t infoSize)
295 {
296 	return _kern_get_image_info(id, info, infoSize);
297 }
298 
299 
300 status_t
301 _get_next_image_info(team_id team, int32 *cookie, image_info *info, size_t infoSize)
302 {
303 	return _kern_get_next_image_info(team, cookie, info, infoSize);
304 }
305 
306 
307 void
308 clear_caches(void *address, size_t length, uint32 flags)
309 {
310 	_kern_clear_caches(address, length, flags);
311 }
312 
313 
314 //	#pragma mark -
315 
316 
317 static char *
318 next_argument(char **_start, bool separate)
319 {
320 	char *line = *_start;
321 	char quote = 0;
322 	int32 i;
323 
324 	// eliminate leading spaces
325 	while (line[0] == ' ')
326 		line++;
327 
328 	if (line[0] == '"' || line[0] == '\'') {
329 		quote = line[0];
330 		line++;
331 	}
332 
333 	if (!line[0])
334 		return NULL;
335 
336 	for (i = 0;; i++) {
337 		if (line[i] == '\\' && line[i + 1] != '\0')
338 			continue;
339 
340 		if (line[i] == '\0') {
341 			*_start = &line[i];
342 			return line;
343 		}
344 		if ((!quote && line[i] == ' ') || line[i] == quote) {
345 			// argument separator!
346 			if (separate)
347 				line[i] = '\0';
348 			*_start = &line[i + 1];
349 			return line;
350 		}
351 	}
352 
353 	return NULL;
354 }
355 
356 
357 status_t
358 __parse_invoke_line(char *invoker, char ***_newArgs,
359 	char * const **_oldArgs, int32 *_argCount, const char *arg0)
360 {
361 	int32 i, count = 0;
362 	char *arg = invoker;
363 	char **newArgs;
364 
365 	// count arguments in the line
366 
367 	while (next_argument(&arg, false)) {
368 		count++;
369 	}
370 
371 	// this is a shell script and requires special treatment
372 	newArgs = (char**)malloc((*_argCount + count + 1) * sizeof(void *));
373 	if (newArgs == NULL)
374 		return B_NO_MEMORY;
375 
376 	// copy invoker and old arguments and to newArgs
377 
378 	for (i = 0; (arg = next_argument(&invoker, true)) != NULL; i++) {
379 		newArgs[i] = arg;
380 	}
381 	for (i = 0; i < *_argCount; i++) {
382 		if (i == 0)
383 			newArgs[i + count] = (char*)arg0;
384 		else
385 			newArgs[i + count] = (char *)(*_oldArgs)[i];
386 	}
387 
388 	newArgs[i + count] = NULL;
389 
390 	*_newArgs = newArgs;
391 	*_oldArgs = (char * const *)newArgs;
392 	*_argCount += count;
393 
394 	return B_OK;
395 }
396 
397 
398 status_t
399 __get_next_image_dependency(image_id id, uint32 *cookie, const char **_name)
400 {
401 	return __gRuntimeLoader->get_next_image_dependency(id, cookie, _name);
402 }
403 
404 
405 status_t
406 __test_executable(const char *path, char *invoker)
407 {
408 	return __gRuntimeLoader->test_executable(path, invoker);
409 }
410 
411 
412 /*!	Allocates a flat buffer and copies the argument and environment strings
413 	into it. The buffer starts with a char* array which contains pointers to
414 	the strings of the arguments and environment, followed by the strings. Both
415 	arguments and environment arrays are NULL-terminated.
416 
417 	If executablePath is non-NULL, it should refer to the executable to be
418 	executed. If the executable file specifies changes to environment variable
419 	values, those will be performed.
420 */
421 status_t
422 __flatten_process_args(const char* const* args, int32 argCount,
423 	const char* const* env, int32* _envCount, const char* executablePath,
424 	char*** _flatArgs, size_t* _flatSize)
425 {
426 	if (args == NULL || _envCount == NULL || (env == NULL && *_envCount != 0))
427 		return B_BAD_VALUE;
428 
429 	int32 envCount = *_envCount;
430 
431 	// determine total needed size
432 	int32 argSize = 0;
433 	for (int32 i = 0; i < argCount; i++) {
434 		if (args[i] == NULL)
435 			return B_BAD_VALUE;
436 		argSize += strlen(args[i]) + 1;
437 	}
438 
439 	int32 envSize = 0;
440 	for (int32 i = 0; i < envCount; i++) {
441 		if (env[i] == NULL)
442 			return B_BAD_VALUE;
443 		envSize += strlen(env[i]) + 1;
444 	}
445 
446 	EnvironmentFilter envFilter;
447 	if (executablePath != NULL)
448 		envFilter.Init(executablePath, env, envCount);
449 
450 	int32 totalSlotCount = argCount + envCount + 2
451 		+ envFilter.AdditionalSlotsNeeded();
452 	int32 size = totalSlotCount * sizeof(char*) + argSize + envSize
453 		+ envFilter.AdditionalSizeNeeded();
454 	if (size > MAX_PROCESS_ARGS_SIZE)
455 		return B_TOO_MANY_ARGS;
456 
457 	// allocate space
458 	char** flatArgs = (char**)malloc(size);
459 	if (flatArgs == NULL)
460 		return B_NO_MEMORY;
461 
462 	char** slot = flatArgs;
463 	char* stringSpace = (char*)(flatArgs + totalSlotCount);
464 
465 	// copy arguments and environment
466 	for (int32 i = 0; i < argCount; i++) {
467 		int32 argSize = strlen(args[i]) + 1;
468 		memcpy(stringSpace, args[i], argSize);
469 		*slot++ = stringSpace;
470 		stringSpace += argSize;
471 	}
472 
473 	*slot++ = NULL;
474 
475 	for (int32 i = 0; i < envCount; i++) {
476 		size_t envSize = envFilter.PrepareSlot(env[i], i, stringSpace);
477 		*slot++ = stringSpace;
478 		stringSpace += envSize;
479 	}
480 
481 	envFilter.PrepareAdditionalSlots(slot, stringSpace);
482 
483 	*slot++ = NULL;
484 
485 	*_envCount = envCount + envFilter.AdditionalSlotsNeeded();
486 	*_flatArgs = flatArgs;
487 	*_flatSize = stringSpace - (char*)flatArgs;
488 	return B_OK;
489 }
490 
491 
492 extern "C" void _call_init_routines_(void);
493 void
494 _call_init_routines_(void)
495 {
496 	// This is called by the original BeOS startup code.
497 	// We don't need it, because our loader already does the job, right?
498 }
499 
500