Skip to content

Commit cb7f5f1

Browse files
Deomid Ryabkovcesantabot
authored andcommitted
ubuntu: Add ASAN build
Build with `--build-var=CC=clang --build-var=ASAN=1` The resulting binary doesn't work, something to do with IPC (the ubuntu-specific part), but it's a start. cc @pimvanpelt CL: ubuntu: Add ASAN build PUBLISHED_FROM=bb637cfb36439c842337a52ef7a1e1ad5d06f8d2
1 parent 4512c4d commit cb7f5f1

File tree

9 files changed

+58
-41
lines changed

9 files changed

+58
-41
lines changed

fw/platforms/stm32/Makefile.build

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -244,11 +244,11 @@ C_CXX_FLAGS = -Os -ggdb -MD -Wall -Wextra -Werror -pipe \
244244
-DFW_ARCHITECTURE=$(APP_PLATFORM) \
245245
$(MGOS_CFLAGS) $(MGOS_FEATURES) \
246246
$(MG_FEATURES_TINY) -DMG_ENABLE_DIRECTORY_LISTING \
247-
-DMG_NET_IF=$(MGOS_MG_NET_IF)
247+
-DMG_NET_IF=$(MGOS_MG_NET_IF) \
248+
$(STM32_CFLAGS)
248249

249-
CFLAGS = -std=gnu99 $(C_CXX_FLAGS) $(STM32_CFLAGS) \
250-
$(APP_CFLAGS)
251-
CXXFLAGS = -std=gnu++11 -fno-exceptions $(C_CXX_FLAGS) $(STM32_CFLAGS) $(APP_CXXFLAGS)
250+
CFLAGS = -std=gnu99 $(C_CXX_FLAGS) $(APP_CFLAGS)
251+
CXXFLAGS = -std=gnu++11 -fno-exceptions $(C_CXX_FLAGS) $(APP_CXXFLAGS)
252252

253253
LDFLAGS += -Wl,--defsym=SRAM_BASE_ADDR=$(SRAM_BASE_ADDR) -Wl,--defsym=SRAM_SIZE=$(SRAM_SIZE) \
254254
-Wl,--defsym=SRAM2_BASE_ADDR=$(SRAM2_BASE_ADDR) -Wl,--defsym=SRAM2_SIZE=$(SRAM2_SIZE) \

fw/platforms/ubuntu/Makefile.build

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,11 @@ BIN_DIR = build/bin
1818
GEN_DIR = $(BUILD_DIR)/gen
1919
FW_STAGING_DIR ?= $(BUILD_DIR)/fw_temp
2020
FW_DIR ?= $(CURDIR)/fw
21-
VERBOSE ?= 0
22-
SSL ?= None
23-
DEBUG ?= 0
24-
CC_WRAPPER ?=
2521
# Extra parts to include in firmware
2622
APP_EXTRA_FW_PARTS ?=
2723

24+
ASAN ?= 0
25+
2826
# Explicitly disable updater, it's not supported on POSIX build yet.
2927
MGOS_ENABLE_DEBUG_UDP = 0
3028
MGOS_ENABLE_BITBANG = 0
@@ -37,6 +35,7 @@ MGOS_POSIX_FEATURES ?= -DMGOS_PROMPT_DISABLE_ECHO -DMGOS_MAX_NUM_UARTS=2 \
3735
MONGOOSE_FEATURES = \
3836
-DMG_USE_READ_WRITE -DMG_ENABLE_THREADS -DMG_ENABLE_THREADS \
3937
-DMG_ENABLE_HTTP_STREAMING_MULTIPART -DMG_DISABLE_DAV \
38+
-DMG_ENABLE_DIRECTORY_LISTING \
4039
-DMG_MODULE_LINES
4140

4241

@@ -51,18 +50,25 @@ MGOS_SRCS = $(notdir $(wildcard *.c)) mgos_init.c \
5150

5251
PLATFORM_SRCS = $(wildcard $(PLATFORM_VPATH)/*.c)
5352

54-
CFLAGS ?= -std=gnu99 -fno-inline -W -Wall -Wundef -Werror -g -Wno-unused-function \
55-
-Wno-missing-field-initializers \
56-
-D_DEFAULT_SOURCE \
57-
$(MGOS_FEATURES) $(MGOS_POSIX_FEATURES) \
58-
$(MONGOOSE_FEATURES) \
59-
-DMGOS_APP=\"$(APP)\" -DFW_ARCHITECTURE=$(APP_PLATFORM) \
60-
$(APP_CFLAGS)
53+
C_CXX_FLAGS = -ggdb -MD -Wall -Wextra -Werror -pipe \
54+
-ffunction-sections -fdata-sections \
55+
-DMGOS_APP=\"$(APP)\" \
56+
-DFW_ARCHITECTURE=$(APP_PLATFORM) \
57+
$(MGOS_FEATURES) $(MGOS_POSIX_FEATURES) \
58+
$(MONGOOSE_FEATURES)
59+
ifeq "$(ASAN)" "1"
60+
C_CXX_FLAGS += -fsanitize=address -fno-omit-frame-pointer
61+
LDFLAGS += -fsanitize=address
62+
else
63+
LDFLAGS += -static
64+
endif
65+
66+
CFLAGS = -std=gnu99 $(C_CXX_FLAGS) $(APP_CFLAGS)
67+
CXXFLAGS = -std=gnu++11 -fno-exceptions $(C_CXX_FLAGS) $(APP_CXXFLAGS)
6168

6269
INCDIRS = $(addprefix -I,$(INCLUDES))
6370
AR ?= ar
64-
LINKER ?= gcc
65-
LDFLAGS ?= -static
71+
LDFLAGS ?=
6672
APP_BIN_LIBS ?=
6773
LIBS ?= pthread cap
6874
LDLIBS = $(addprefix -l,$(LIBS))
@@ -130,9 +136,9 @@ $(FS_STAGING_DIR): $(FS_FILES)
130136
# Objects of all sorts
131137
-include $(wildcard $(BUILD_DIR)/*.d)
132138
$(OBJS): $(BUILD_DIR)/%.o : %.c $(GENFILES_LIST)
133-
$(Q) mkdir -p $(shell dirname $(OBJS))
134-
$(vecho) "CC $< -> $@"
135-
$(Q) $(CC_WRAPPER) $(CC) -MD $(INCDIRS) $(CFLAGS) -c $< -o $@
139+
$(vecho) "CC $<"
140+
$(Q) mkdir -p $(dir $@)
141+
$(Q) $(CC) $(CFLAGS) $(INCDIRS) -c $< -o $@
136142

137143
# lib.a, used during lib release build
138144
$(BUILD_DIR)/$(BUILD_LIB_NAME): $(BUILD_DIR) $(GENFILES_LIST) $(APP_OBJS)
@@ -142,7 +148,9 @@ $(BUILD_DIR)/$(BUILD_LIB_NAME): $(BUILD_DIR) $(GENFILES_LIST) $(APP_OBJS)
142148
# Application target.
143149
$(APP_BIN): $(BIN_DIR) $(OBJS)
144150
$(vecho) "LD $@"
145-
$(Q) $(CC_WRAPPER) $(LINKER) -Wl,--start-group $(OBJS) $(APP_BIN_LIBS) $(LDLIBS) -Wl,--end-group $(LDFLAGS) -o $(BIN_DIR)/$(APP)
151+
$(Q) $(CC) -Wl,--gc-sections -Wl,-Map=$@.map \
152+
-Wl,--start-group $(OBJS) $(APP_BIN_LIBS) $(LDLIBS) -Wl,--end-group \
153+
$(LDFLAGS) -o $(BIN_DIR)/$(APP)
146154

147155
$(APP_ELF): $(APP_BIN)
148156
$(Q) cp -v $(APP_BIN) $@

fw/platforms/ubuntu/sdk.version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
docker.io/mgos/ubuntu-build:bionic-r2
1+
docker.io/mgos/ubuntu-build:bionic-r3

fw/platforms/ubuntu/src/ubuntu_cap.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,9 @@ bool ubuntu_cap_init(void) {
7171
LOGM(LL_ERROR, ("Cannot chroot to %s", Flags.chroot));
7272
return false;
7373
}
74-
chdir("/");
74+
if (chdir("/") != 0) {
75+
return false;
76+
}
7577
LOGM(LL_INFO, ("Setting chroot=%s", Flags.chroot));
7678
}
7779

fw/platforms/ubuntu/src/ubuntu_gpio.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ static struct mgos_gpio_state *s_state = NULL;
5050
static int s_num_gpio_states = 0;
5151
struct mgos_rlock_type *s_lock = NULL;
5252

53-
static void mgos_gpio_int_cb(void *arg);
5453
static void mgos_gpio_dbnc_done_cb(void *arg);
5554

5655
static IRAM struct mgos_gpio_state *mgos_gpio_get_state(int pin) {
@@ -85,7 +84,7 @@ static struct mgos_gpio_state *mgos_gpio_get_or_create_state(int pin) {
8584
}
8685

8786
/* In MGOS task context. */
88-
static void mgos_gpio_int_cb(void *arg) {
87+
void mgos_gpio_int_cb(void *arg) {
8988
int pin = (intptr_t) arg;
9089

9190
mgos_rlock(s_lock);

fw/platforms/ubuntu/src/ubuntu_ipc_client.c

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ extern struct ubuntu_pipe s_pipe;
2424

2525
static bool ubuntu_ipc_cmd(const struct ubuntu_pipe_message *in,
2626
struct ubuntu_pipe_message *out) {
27-
size_t _len;
27+
ssize_t len;
2828
bool ret = false;
2929
struct msghdr msg;
3030
struct iovec iov[1];
@@ -40,19 +40,19 @@ static bool ubuntu_ipc_cmd(const struct ubuntu_pipe_message *in,
4040
msg.msg_iovlen = 1;
4141

4242
mgos_rlock(s_pipe.lock);
43-
_len = sendmsg(s_pipe.mongoose_fd, &msg, 0);
44-
if (_len < 2) {
45-
LOG(LL_ERROR, ("Cannot write message"));
43+
len = sendmsg(s_pipe.mongoose_fd, &msg, 0);
44+
if (len < 2) {
45+
LOG(LL_ERROR, ("Cannot write message %d %d", (int) len, errno));
4646
goto exit;
4747
}
4848

4949
iov[0].iov_base = (void *) out;
5050
iov[0].iov_len = sizeof(struct ubuntu_pipe_message);
5151
msg.msg_iov = iov;
5252
msg.msg_iovlen = 1;
53-
_len = recvmsg(s_pipe.mongoose_fd, &msg, 0);
54-
if (_len < 2) {
55-
LOG(LL_ERROR, ("Cannot read message"));
53+
len = recvmsg(s_pipe.mongoose_fd, &msg, 0);
54+
if (len < 2) {
55+
LOG(LL_ERROR, ("Cannot read message %d %d", (int) len, errno));
5656
goto exit;
5757
}
5858

@@ -62,12 +62,12 @@ static bool ubuntu_ipc_cmd(const struct ubuntu_pipe_message *in,
6262
}
6363

6464
int ubuntu_ipc_open(const char *pathname, int flags) {
65-
size_t _len;
65+
ssize_t len;
6666
struct msghdr msg;
6767
struct iovec iov[1];
6868
struct ubuntu_pipe_message iovec_payload;
6969

70-
union {
70+
static union {
7171
struct cmsghdr cm;
7272
char control[CMSG_SPACE(sizeof(int))];
7373
} control_un;
@@ -94,9 +94,9 @@ int ubuntu_ipc_open(const char *pathname, int flags) {
9494
msg.msg_iovlen = 1;
9595

9696
mgos_rlock(s_pipe.lock);
97-
_len = sendmsg(s_pipe.mongoose_fd, &msg, 0);
98-
if (_len < 2) {
99-
LOG(LL_ERROR, ("Cannot write message"));
97+
len = sendmsg(s_pipe.mongoose_fd, &msg, 0);
98+
if (len < 2) {
99+
LOG(LL_ERROR, ("Cannot write message %d", (int) len));
100100
goto exit;
101101
}
102102

@@ -108,8 +108,8 @@ int ubuntu_ipc_open(const char *pathname, int flags) {
108108
msg.msg_control = control_un.control;
109109
msg.msg_controllen = sizeof(control_un.control);
110110

111-
_len = recvmsg(s_pipe.mongoose_fd, &msg, 0);
112-
if (_len < 2) {
111+
len = recvmsg(s_pipe.mongoose_fd, &msg, 0);
112+
if (len < 2) {
113113
LOG(LL_ERROR, ("Cannot read message"));
114114
goto exit;
115115
}

fw/src/mgos_sys_config.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ static bool load_config_defaults_internal(struct mgos_config *cfg,
9797
int i;
9898
char fname[sizeof(CONF_USER_FILE) + 10];
9999
memset(cfg, 0, sizeof(*cfg));
100-
memcpy(fname, CONF_USER_FILE, sizeof(fname));
100+
memcpy(fname, CONF_USER_FILE, sizeof(CONF_USER_FILE));
101101
const char *acl = "*";
102102
for (i = 0; i < MGOS_CONFIG_LEVEL_USER; i++) {
103103
fname[CONF_USER_FILE_NUM_IDX] = '0' + i;

fw/src/mgos_time.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,21 @@
1515
* limitations under the License.
1616
*/
1717

18+
#ifndef __TI_COMPILER_VERSION__
19+
#include <unistd.h>
20+
#endif
21+
1822
#include "mgos.h"
1923
#include "mgos_event.h"
2024
#include "mgos_hal.h"
2125
#include "mgos_mongoose.h"
2226

2327
#include "common/queue.h"
2428

29+
#if defined(_DEFAULT_SOURCE) || defined(_BSD_SOURCE)
30+
#include <sys/time.h>
31+
#endif
32+
2533
double mgos_uptime(void) {
2634
return mgos_uptime_micros() / 1000000.0;
2735
}

fw/tools/docker/ubuntu/Dockerfile-ubuntu-build

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ RUN DEBIAN_FRONTEND=noninteractive apt-get update && \
1010
apt-get clean
1111

1212
RUN DEBIAN_FRONTEND=noninteractive apt-get install -y -q \
13-
libcap-dev && \
13+
clang libcap-dev valgrind && \
1414
apt-get clean
1515

1616
RUN useradd -d /opt -m -s /bin/bash user && chown -R user /opt

0 commit comments

Comments
 (0)