base: remove internal use of format strings

Issue #2064
This commit is contained in:
Norman Feske 2023-03-06 10:57:13 +01:00 committed by Christian Helmuth
parent 9debad4e91
commit 915adcd0dd
20 changed files with 101 additions and 144 deletions

View File

@ -117,8 +117,7 @@ inline int lx_ioctl_irq(int fd, int irq)
** Process creation and destruction **
**************************************/
inline int lx_execve(const char *filename, char *const argv[],
char *const envp[])
inline int lx_execve(char const *filename, char const *argv[], char const *envp[])
{
return (int)lx_syscall(SYS_execve, filename, argv, envp);
}

View File

@ -18,7 +18,7 @@
#include <linux_syscalls.h>
/* Genode includes */
#include <base/snprintf.h>
#include <util/string.h>
/**
* Return resource path for Genode
@ -27,18 +27,8 @@
*/
static inline char const *resource_path()
{
struct Resource_path
{
char string[32];
Resource_path()
{
Genode::snprintf(string, sizeof(string), "/tmp/genode-%d", lx_getuid());
}
};
static Resource_path path;
return path.string;
static Genode::String<32> path("/tmp/genode-", lx_getuid());
return path.string();
}
#endif /* _CORE__INCLUDE__RESOURCE_PATH_H_ */

View File

@ -13,7 +13,6 @@
/* Genode includes */
#include <util/arg_string.h>
#include <base/snprintf.h>
#include <cpu/consts.h>
/* core includes */
@ -42,8 +41,8 @@ struct Execve_args_and_stack
struct Args
{
char const *filename;
char **argv;
char **envp;
char const **argv;
char const **envp;
Lx_sd parent_sd;
};
@ -111,8 +110,7 @@ void Native_pd_component::_start(Dataspace_component &ds)
const char *tmp_filename = "temporary_executable_elf_dataspace_file_for_execve";
/* we need 's' on stack to make it an lvalue with an lvalue member we use the pointer to */
Linux_dataspace::Filename s = ds.fname();
const char *filename = s.buf;
Linux_dataspace::Filename filename = ds.fname();
/*
* In order to be executable via 'execve', a program must be represented as
@ -121,11 +119,11 @@ void Native_pd_component::_start(Dataspace_component &ds)
* the dataspace content into a temporary file whose path is passed to
* 'execve()'.
*/
if (Genode::strcmp(filename, "") == 0) {
if (filename == "") {
filename = tmp_filename;
int tmp_binary_fd = lx_open(filename, O_CREAT | O_EXCL | O_WRONLY, S_IRWXU);
int tmp_binary_fd = lx_open(filename.string(), O_CREAT | O_EXCL | O_WRONLY, S_IRWXU);
if (tmp_binary_fd < 0) {
error("Could not create file '", filename, "'");
return; /* XXX reflect error to client */
@ -141,30 +139,24 @@ void Native_pd_component::_start(Dataspace_component &ds)
}
/* pass parent capability as environment variable to the child */
enum { ENV_STR_LEN = 256 };
static char envbuf[5][ENV_STR_LEN];
Genode::snprintf(envbuf[1], ENV_STR_LEN, "parent_local_name=%lu",
_pd_session._parent.local_name());
Genode::snprintf(envbuf[2], ENV_STR_LEN, "DISPLAY=%s",
get_env("DISPLAY"));
Genode::snprintf(envbuf[3], ENV_STR_LEN, "HOME=%s",
get_env("HOME"));
Genode::snprintf(envbuf[4], ENV_STR_LEN, "LD_LIBRARY_PATH=%s",
get_env("LD_LIBRARY_PATH"));
char *env[] = { &envbuf[0][0], &envbuf[1][0], &envbuf[2][0],
&envbuf[3][0], &envbuf[4][0], 0 };
using Env_string = String<256>;
static Env_string env_strings[] {
{ "parent_local_name=", _pd_session._parent.local_name() },
{ "DISPLAY=", get_env("DISPLAY") },
{ "HOME=", get_env("HOME") },
{ "LD_LIBRARY_PATH=", get_env("LD_LIBRARY_PATH") },
};
char const *env[] = { env_strings[0].string(), env_strings[1].string(),
env_strings[2].string(), env_strings[3].string(),
nullptr };
/* prefix name of Linux program (helps killing some zombies) */
char const *prefix = "[Genode] ";
char pname_buf[sizeof(_pd_session._label) + sizeof(prefix)];
snprintf(pname_buf, sizeof(pname_buf), "%s%s", prefix, _pd_session._label.string());
char *argv_buf[2];
argv_buf[0] = pname_buf;
argv_buf[1] = 0;
using Pname = String<Session::Label::capacity() + 9>;
Pname const pname("[Genode] ", _pd_session._label);
char const *argv_buf[] { pname.string(), nullptr };
_execve_args_and_stack().args = Execve_args_and_stack::Args {
.filename = filename,
.filename = filename.string(),
.argv = argv_buf,
.envp = env,
.parent_sd = Capability_space::ipc_cap_data(_pd_session._parent).dst.socket
@ -173,8 +165,8 @@ void Native_pd_component::_start(Dataspace_component &ds)
_pid = lx_create_process((int (*)())_exec_child,
_execve_args_and_stack().initial_sp());
if (Genode::strcmp(filename, tmp_filename) == 0)
lx_unlink(filename);
if (filename == tmp_filename)
lx_unlink(filename.string());
}

View File

@ -14,9 +14,6 @@
/* glibc includes */
#include <fcntl.h>
/* Genode includes */
#include <base/snprintf.h>
/* local includes */
#include <ram_dataspace_factory.h>
#include <resource_path.h>
@ -35,12 +32,11 @@ static int ram_ds_cnt = 0; /* counter for creating unique dataspace IDs */
void Ram_dataspace_factory::_export_ram_ds(Dataspace_component &ds)
{
char fname[Linux_dataspace::FNAME_LEN];
Linux_dataspace::Filename const fname(resource_path(), "/ds-", ram_ds_cnt++);
/* create file using a unique file name in the resource path */
snprintf(fname, sizeof(fname), "%s/ds-%d", resource_path(), ram_ds_cnt++);
lx_unlink(fname);
int const fd = lx_open(fname, O_CREAT|O_RDWR|O_TRUNC|LX_O_CLOEXEC, S_IRWXU);
lx_unlink(fname.string());
int const fd = lx_open(fname.string(), O_CREAT|O_RDWR|O_TRUNC|LX_O_CLOEXEC, S_IRWXU);
lx_ftruncate(fd, ds.size());
/* remember file descriptor in dataspace component object */
@ -52,7 +48,7 @@ void Ram_dataspace_factory::_export_ram_ds(Dataspace_component &ds)
* gone (i.e., an open file descriptor referring to the file). A process
* w/o the right file descriptor won't be able to open and access the file.
*/
lx_unlink(fname);
lx_unlink(fname.string());
}

View File

@ -21,13 +21,10 @@
/* Genode includes */
#include <linux_dataspace/linux_dataspace.h>
#include <util/arg_string.h>
#include <util/misc_math.h>
#include <root/root.h>
#include <base/session_label.h>
/* local includes */
#include "dataspace_component.h"
#include <dataspace_component.h>
using namespace Core;
@ -35,17 +32,15 @@ using namespace Core;
Linux_dataspace::Filename Dataspace_component::_file_name(const char *args)
{
Session_label const label = label_from_args(args);
Linux_dataspace::Filename fname;
if (label.last_element().length() > sizeof(fname.buf)) {
if (label.last_element().length() > Linux_dataspace::Filename::capacity()) {
error("file name too long: ", label.last_element());
throw Service_denied();
}
copy_cstring(fname.buf, label.last_element().string(), sizeof(fname.buf));
Linux_dataspace::Filename const fname = label.last_element();
/* only files inside the current working directory are allowed */
for (const char *c = fname.buf; *c; ++c)
for (const char *c = fname.string(); *c; ++c)
if (*c == '/') throw Service_denied();
return fname;
@ -55,7 +50,7 @@ Linux_dataspace::Filename Dataspace_component::_file_name(const char *args)
size_t Dataspace_component::_file_size()
{
Genode::uint64_t size = 0;
if (lx_stat_size(_fname.buf, size) < 0)
if (lx_stat_size(_fname.string(), size) < 0)
throw Service_denied();
return align_addr((size_t)size, 12);
@ -67,7 +62,7 @@ Dataspace_component::Dataspace_component(const char *args)
_fname(_file_name(args)),
_size(_file_size()),
_addr(0),
_cap(_fd_to_cap(lx_open(_fname.buf, O_RDONLY | LX_O_CLOEXEC, S_IRUSR | S_IXUSR))),
_cap(_fd_to_cap(lx_open(_fname.string(), O_RDONLY | LX_O_CLOEXEC, S_IRUSR | S_IXUSR))),
_writeable(false),
_owner(0)
{ }
@ -79,5 +74,4 @@ Dataspace_component::Dataspace_component(size_t size, addr_t, addr_t phys_addr,
_size(size), _addr(phys_addr), _cap(), _writeable(false), _owner(_owner)
{
warning("Should only be used for IOMEM and not within Linux.");
_fname.buf[0] = 0;
}

View File

@ -16,16 +16,15 @@
#include <dataspace/dataspace.h>
#include <base/stdint.h>
#include <base/ipc.h>
#include <base/rpc.h>
#include <base/ipc.h>
namespace Genode { struct Linux_dataspace; }
struct Genode::Linux_dataspace : Dataspace
{
enum { FNAME_LEN = 64 };
struct Filename { char buf[FNAME_LEN]; };
using Filename = String<64>;
virtual ~Linux_dataspace() { }
@ -42,11 +41,11 @@ struct Genode::Linux_dataspace : Dataspace
*/
virtual Untyped_capability fd() = 0;
/*********************
** RPC declaration **
*********************/
GENODE_RPC(Rpc_fname, Filename, fname);
GENODE_RPC(Rpc_fd, Untyped_capability, fd);
GENODE_RPC_INTERFACE_INHERIT(Dataspace, Rpc_fname, Rpc_fd);

View File

@ -15,7 +15,6 @@
/* Genode includes */
#include <base/env.h>
#include <base/thread.h>
#include <base/snprintf.h>
#include <base/sleep.h>
#include <base/log.h>
#include <linux_native_cpu/client.h>

View File

@ -36,7 +36,6 @@
/* Genode includes */
#include <util/string.h>
#include <base/snprintf.h>
#include <base/log.h>
#include <base/sleep.h>
@ -77,14 +76,6 @@
extern "C" void wait_for_continue(void);
#define PRAW(fmt, ...) \
do { \
char str[128]; \
Genode::snprintf(str, sizeof(str), \
ESC_ERR fmt ESC_END "\n", ##__VA_ARGS__); \
Genode::raw(Genode::Cstring(str)); \
} while (0)
/*********************************************************
** System-call bindings implemented in syscall library **

View File

@ -15,9 +15,7 @@
#include <base/heap.h>
#include <base/thread.h>
#include <base/log.h>
#include <base/snprintf.h>
#include <base/component.h>
#include <util/touch.h>
#include <util/retry.h>
#include <rm_session/connection.h>
@ -625,24 +623,18 @@ class Greedy : public Genode::Thread {
}
};
void check(uint8_t res, const char *format, ...)
template <typename... ARGS>
void check(uint8_t res, ARGS &&... args)
{
static char buf[128];
va_list list;
va_start(list, format);
String_console sc(buf, sizeof(buf));
sc.vprintf(format, list);
va_end(list);
String<128> msg(args...);
if (res == Nova::NOVA_OK) {
error("res=", res, " ", Cstring(buf), " - TEST FAILED");
error("res=", res, " ", msg, " - TEST FAILED");
failed++;
}
else
log("res=", res, " ", Cstring(buf));
log("res=", res, " ", msg);
}
struct Main
@ -708,7 +700,7 @@ Main::Main(Env &env) : env(env)
for (unsigned i = 0; i < (1U << Nova::NUM_INITIAL_PT_LOG2); i++) {
addr_t sel_exc = myself->native_thread().exc_pt_sel + i;
res = Nova::pt_ctrl(sel_exc, 0xbadbad);
check(res, "pt_ctrl %2u", i);
check(res, "pt_ctrl ", i);
}
/* test PAT kernel feature */

View File

@ -18,18 +18,16 @@
#define _INCLUDE__BASE__INTERNAL__ASSERT_H_
/* Genode includes */
#include <base/snprintf.h>
#include <util/string.h>
/* base-internal includes */
#include <base/internal/kernel_debugger.h>
#define ASSERT(e) \
do { if (!(e)) { \
char line_buf[32]; \
Genode::snprintf(line_buf, sizeof(line_buf), "%d", __LINE__); \
kernel_debugger_outstring("Assertion failed: " #e "\n"); \
kernel_debugger_outstring(__FILE__ ":"); \
kernel_debugger_outstring(line_buf); \
kernel_debugger_outstring(Genode::String<32>(__LINE__).string()); \
kernel_debugger_panic("\n"); \
} \
} while(0)

View File

@ -18,6 +18,7 @@
#include <base/env.h>
#include <base/capability.h>
#include <base/log.h>
#include <base/snprintf.h> /* deprecated */
namespace Genode {

View File

@ -15,7 +15,6 @@
#ifndef _INCLUDE__BASE__SESSION_LABEL_H_
#define _INCLUDE__BASE__SESSION_LABEL_H_
#include <base/snprintf.h>
#include <util/arg_string.h>
#include <util/string.h>
@ -40,8 +39,7 @@ struct Genode::Session_label : String<160>
* copy constructors as candidate.
*/
template <size_t N>
Session_label(Genode::String<N> const &other)
: Genode::String<160>(other) { }
Session_label(Genode::String<N> const &other) : Genode::String<160>(other) { }
Session_label last_element() const
{

View File

@ -24,7 +24,6 @@
#include <cpu_session/capability.h>
#include <pd_session/capability.h>
#include <base/allocator.h>
#include <base/snprintf.h>
namespace Genode {

View File

@ -29,7 +29,6 @@
#include <util/token.h>
#include <util/string.h>
#include <base/snprintf.h>
namespace Genode {
@ -329,10 +328,8 @@ class Genode::Arg_string
static bool set_arg(char *args, size_t args_len,
const char *key, int value)
{
enum { STRING_LONG_MAX = 32 };
char buf[STRING_LONG_MAX];
snprintf(buf, sizeof(buf), "%d", value);
return remove_arg(args, key) && add_arg(args, args_len, key, buf);
return remove_arg(args, key)
&& add_arg(args, args_len, key, String<16>(value).string());
}
/**

View File

@ -16,7 +16,6 @@
#include <util/string.h>
#include <util/print_lines.h>
#include <base/snprintf.h>
namespace Genode { class Xml_generator; }
@ -336,9 +335,7 @@ class Genode::Xml_generator
void attribute(char const *name, long long value)
{
char buf[64];
Genode::snprintf(buf, sizeof(buf), "%lld", value);
_curr_node->insert_attribute(name, buf);
_curr_node->insert_attribute(name, String<64>(value).string());
}
void attribute(char const *name, long value)
@ -353,9 +350,7 @@ class Genode::Xml_generator
void attribute(char const *name, unsigned long long value)
{
char buf[64];
Genode::snprintf(buf, sizeof(buf), "%llu", value);
_curr_node->insert_attribute(name, buf);
_curr_node->insert_attribute(name, String<64>(value).string());
}
void attribute(char const *name, unsigned long value)

View File

@ -100,8 +100,8 @@ grep_output {\[init -\> test-smp\] Affinity: Round}
set rounds "10"
set good_string {}
for {set r 0} {$r <= $rounds} {incr r} {
append good_string {[init -> test-smp] Affinity: Round }
append good_string [format "%02d" $r]
append good_string {[init -> test-smp] Affinity: Round }
append good_string [format "%2d" $r]
append good_string ":"
for {set i 0} {$i < $cpus} {incr i} {
append good_string " A"

View File

@ -12,7 +12,6 @@
*/
/* Genode includes */
#include <base/snprintf.h>
#include <base/sleep.h>
#include <base/service.h>
#include <base/child.h>

View File

@ -18,7 +18,6 @@
#include <base/thread.h>
#include <base/env.h>
#include <base/sleep.h>
#include <base/snprintf.h>
#include <deprecated/env.h>
/* base-internal includes */
@ -133,7 +132,7 @@ void Thread::_free_stack(Stack *stack)
void Thread::name(char *dst, size_t dst_len)
{
snprintf(dst, dst_len, "%s", _stack->name().string());
copy_cstring(dst, name().string(), dst_len);
}

View File

@ -19,6 +19,7 @@
#include <base/log.h>
#include <base/rpc_server.h>
#include <base/rpc_client.h>
#include <util/formatted_output.h>
#include <trace/timestamp.h>
@ -203,42 +204,59 @@ namespace Affinity_test {
volatile uint64_t cnt = 0;
unsigned round = 0;
char const text_cpu[] = "Affinity: CPU: ";
char const text_round[] = "Affinity: Round %2u: ";
char * output_buffer = new (heap) char [sizeof(text_cpu) + 3 * cpus.total()];
static char const text_cpu[] = "Affinity: CPU: ";
for (; round < 11;) {
cnt++;
/* try to get a life sign by the main thread from the remote threads */
if (cnt % COUNT_VALUE == 0) {
char * output = output_buffer;
snprintf(output, sizeof(text_cpu), text_cpu);
output += sizeof(text_cpu) - 1;
for (unsigned i = 0; i < cpus.total(); i++) {
snprintf(output, 4, "%2u ", i);
output += 3;
}
log(Cstring(output_buffer));
output = output_buffer;
snprintf(output, sizeof(text_round), text_round, round);
output += sizeof(text_round) - 2;
struct Table_header
{
unsigned num_cpus;
for (unsigned i = 0; i < cpus.total(); i++) {
snprintf(output, 4, "%s ",
thread_cnt[i] == threads[i]->cnt ? " D" : " A");
output += 3;
void print(Output &out) const
{
using Genode::print;
print(out, text_cpu);
for (unsigned i = 0; i < num_cpus; i++)
print(out, Right_aligned(2, i), " ");
}
};
struct Table_entries
{
unsigned num_cpus;
unsigned round;
Spinning_thread **threads;
uint64_t *thread_cnt;
void print(Output &out) const
{
using Genode::print;
print(out, "Affinity: Round ", Right_aligned(2, round), ": ");
for (unsigned i = 0; i < num_cpus; i++)
print(out, thread_cnt[i] == threads[i]->cnt ? " D " : " A ");
}
};
log(Table_header { .num_cpus = cpus.total() });
log(Table_entries { .num_cpus = cpus.total(),
.round = round,
.threads = threads,
.thread_cnt = thread_cnt });
for (unsigned i = 0; i < cpus.total(); i++)
thread_cnt[i] = threads[i]->cnt;
}
log(Cstring(output_buffer));
round ++;
}
}
destroy(heap, output_buffer);
for (unsigned i = 0; i < cpus.total(); i++)
destroy(heap, threads[i]);
destroy(heap, threads);

View File

@ -25,6 +25,9 @@
#include <cpu_thread/client.h>
#include <cpu/memory_barrier.h>
/* compiler includes */
#include <stdarg.h>
using namespace Genode;
@ -194,7 +197,7 @@ struct Cpu_helper : Thread
{
Env &_env;
Cpu_helper(Env &env, const char * name, Cpu_session &cpu)
Cpu_helper(Env &env, Name const &name, Cpu_session &cpu)
:
Thread(env, name, STACK_SIZE, Thread::Location(), Thread::Weight(), cpu),
_env(env)
@ -306,7 +309,6 @@ static void test_create_as_many_threads(Env &env)
Thread::stack_virtual_size();
Cpu_helper * threads[max];
static char thread_name[8];
Heap heap(env.ram(), env.rm());
@ -314,8 +316,7 @@ static void test_create_as_many_threads(Env &env)
try {
for (; i < max; i++) {
try {
snprintf(thread_name, sizeof(thread_name), "%u", i + 1);
threads[i] = new (heap) Cpu_helper(env, thread_name, env.cpu());
threads[i] = new (heap) Cpu_helper(env, Thread::Name(i + 1), env.cpu());
threads[i]->start();
threads[i]->join();
} catch (Cpu_session::Thread_creation_failed) {