Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests/multiboot #18

Merged
merged 5 commits into from
Feb 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ peda-session-*
!.codecov.yml

tests/test-allocator
tests/test-mock-bhyveload
tests/test-mock-bhyveload
tests/test-multiboot
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ script:
- docker run -i fubarnetes/bhyve-multiboot-ci build CC=${CC}

after_success:
- docker run -i fubarnetes/bhyve-multiboot-ci bash -c "build && coverage ${CODECOV_TOKEN}"
- ci_env=`bash <(curl -s https://codecov.io/env)`
- docker run ${ci_env} -i fubarnetes/bhyve-multiboot-ci bash -c "build && coverage"

notifications:
webhooks:
Expand Down
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ CFLAGS+= -g
OBJS= loader.o multiboot.o allocator.o

LIBELF= -lelf
.export LIBELF

.PHONY: all
all: libmultiboot.so libmultiboot.so.full libmultiboot.a libmultiboot_p.a
Expand Down
2 changes: 1 addition & 1 deletion ci/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ ADD ./ci/install_kyua.sh /ci/install_kyua.sh
RUN sh /ci/install_kyua.sh

RUN apt-get install -qq freebsd-buildutils bmake libelf-freebsd-dev
RUN apt-get install -qq gcc-7 clang-5.0
RUN apt-get install -qq gcc-7 clang-5.0 lib32gcc-7-dev
RUN apt-get install -qq curl

ADD . /build
Expand Down
1 change: 1 addition & 0 deletions tests/Kyuafile
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ test_suite("libmultiboot")

atf_test_program{name="test-allocator", }
atf_test_program{name="test-mock-bhyveload", }
atf_test_program{name="test-multiboot", }
35 changes: 33 additions & 2 deletions tests/Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
LDFLAGS=-L/usr/local/lib
OBJCOPY?=objcopy

LIBELF= -lelf
LIBATF_C=/usr/local/lib/libatf-c.a

LDFLAGS=-L/usr/local/lib

UNAME_S!=uname -s
OUTPUT_BFD=elf64-x86-64
.if ${UNAME_S:MFreeBSD}
OUTPUT_BFD=elf64-x86-64-freebsd
.endif

MB_DIR=data/multiboot
MB_TESTCASES=data_multiboot_mmap.o data_multiboot_modules.o

.PHONY: all
all: test-allocator test-loader test-mock-bhyveload
all: test-allocator test-loader test-mock-bhyveload test-multiboot

.PHONY: check
check: all
Expand All @@ -11,12 +24,30 @@ check: all

.PHONY: clean
clean:
$(MAKE) -C data/multiboot clean
rm -f test-allocator.o
rm -f test-mock-bhyveload.o
rm -f mock/bhyveload.o
rm -f test-allocator
rm *.gcda *.gcno *.gcov

.PHONY: ${MB_DIR}/mmap.elf
${MB_DIR}/mmap.elf:
$(MAKE) -C data/multiboot mmap.elf

.PHONY: ${MB_DIR}/modules.elf
${MB_DIR}/modules.elf:
$(MAKE) -C data/multiboot modules.elf

data_multiboot_mmap.o: ${MB_DIR}/mmap.elf
$(OBJCOPY) --input binary --binary-architecture i386 --output ${OUTPUT_BFD} $> $@

data_multiboot_modules.o: ${MB_DIR}/modules.elf
$(OBJCOPY) --input binary --binary-architecture i386 --output ${OUTPUT_BFD} $> $@

test-multiboot: test-multiboot.o ${MB_TESTCASES} ../libmultiboot_p.a
$(CC) ${LDFLAGS} ${CFLAGS} -o $@ $> ${LIBATF_C} ${LIBELF}

test-mock-bhyveload: test-mock-bhyveload.o mock/bhyveload.o mock/compat-strlcat.o mock/compat-strlcpy.o
$(CC) ${LDFLAGS} ${CFLAGS} -o $@ $> ${LIBATF_C} ${LIBELF}

Expand Down
42 changes: 42 additions & 0 deletions tests/data-multiboot.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright (c) 2018, Fabian Freyer <[email protected]>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the <organization> nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#pragma once

#pragma once

#define MB_TESTCASE(name) \
extern char _binary_data_multiboot_ ## name ## _elf_end; \
extern char _binary_data_multiboot_ ## name ## _elf_start; \

#define MB_TESTDATA_SYMBOL(name, suffix) _binary_data_multiboot_ ## name ## _elf_ ## suffix
#define MB_TESTDATA_START(name) &MB_TESTDATA_SYMBOL(name, start)
#define MB_TESTDATA_END(name) &MB_TESTDATA_SYMBOL(name, end)
#define MB_TESTDATA_SIZE(name) (MB_TESTDATA_START(name) - MB_TESTDATA_END(name))

MB_TESTCASE(mmap)
MB_TESTCASE(modules)
33 changes: 33 additions & 0 deletions tests/data/multiboot/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
CCFLAGS=-m32 -Wall -Wextra -Werror -fno-stack-protector -nostdinc -fno-builtin
ASFLAGS=-m32

UNAME_S!=uname -s
LIBGCC!=$(CC) ${CCFLAGS} -print-libgcc-file-name

LDEMULATION=elf_i386
.if ${UNAME_S:MFreeBSD}
LDEMULATION=elf_i386_fbsd
.endif

LDFLAGS=-m${LDEMULATION} -T link.ld

.PHONY: all
all: mmap.elf modules.elf

.PHONY: clean
clean:
rm -f start.o mmap.o libc.o test-multiboot.o
rm -f mmap.elf
rm -f modules.elf

mmap.elf: start.o mmap.o libc.o
$(LD) $(LDFLAGS) -o $@ $> $(LIBGCC)

modules.elf: start.o modules.o libc.o
$(LD) $(LDFLAGS) -o $@ $> $(LIBGCC)

.c.o:
$(CC) $(CCFLAGS) -c -o $@ $<

.S.o:
$(CC) $(ASFLAGS) -c -o $@ $<
161 changes: 161 additions & 0 deletions tests/data/multiboot/libc.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
/*
* Copyright (c) 2013 Kevin Wolf <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

#include "libc.h"

void* memcpy(void *dest, const void *src, int n)
{
char *d = dest;
const char *s = src;

while (n--) {
*d++ = *s++;
}

return dest;
}

void* memset(void *b, int c, unsigned len)
{
char *bb;

for (bb = (char *)b; len--; )
*bb++ = c;

return (b);
}

static void print_char(char c)
{
outb(0xe9, c);
}

static void print_str(char *s)
{
while (*s) {
print_char(*s++);
}
}

static void print_num(uint64_t value, int base)
{
char digits[] = "0123456789abcdef";
char buf[32] = { 0 };
int i = sizeof(buf) - 2;

do {
buf[i--] = digits[value % base];
value /= base;
} while (value);

print_str(&buf[i + 1]);
}

void printf(const char *fmt, ...)
{
va_list ap;
uint64_t val;
char *str;
int base;
int has_long;
int alt_form;

va_start(ap, fmt);

for (; *fmt; fmt++) {
if (*fmt != '%') {
print_char(*fmt);
continue;
}
fmt++;

if (*fmt == '#') {
fmt++;
alt_form = 1;
} else {
alt_form = 0;
}

if (*fmt == 'l') {
fmt++;
if (*fmt == 'l') {
fmt++;
has_long = 2;
} else {
has_long = 1;
}
} else {
has_long = 0;
}

switch (*fmt) {
case 'x':
case 'p':
base = 16;
goto convert_number;
case 'd':
case 'i':
case 'u':
base = 10;
goto convert_number;
case 'o':
base = 8;
goto convert_number;

convert_number:
switch (has_long) {
case 0:
val = va_arg(ap, unsigned int);
break;
case 1:
val = va_arg(ap, unsigned long);
break;
case 2:
val = va_arg(ap, unsigned long long);
break;
}

if (alt_form && base == 16) {
print_str("0x");
}

print_num(val, base);
break;

case 's':
str = va_arg(ap, char*);
print_str(str);
break;
case '%':
print_char(*fmt);
break;
default:
print_char('%');
print_char(*fmt);
break;
}
}

va_end(ap);
}


63 changes: 63 additions & 0 deletions tests/data/multiboot/libc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright (c) 2013 Kevin Wolf <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

#ifndef LIBC_H
#define LIBC_H

/* Integer types */

typedef unsigned long long uint64_t;
typedef unsigned int uint32_t;
typedef unsigned short uint16_t;
typedef unsigned char uint8_t;

typedef signed long long int64_t;
typedef signed int int32_t;
typedef signed short int16_t;
typedef signed char int8_t;

typedef uint32_t uintptr_t;


/* stdarg.h */

typedef __builtin_va_list va_list;
#define va_start(ap, X) __builtin_va_start(ap, X)
#define va_arg(ap, type) __builtin_va_arg(ap, type)
#define va_end(ap) __builtin_va_end(ap)


/* Port I/O functions */

static inline void outb(uint16_t port, uint8_t data)
{
asm volatile ("outb %0, %1" : : "a" (data), "Nd" (port));
}


/* Misc functions */

void printf(const char *fmt, ...);
void* memcpy(void *dest, const void *src, int n);
void* memset(void *b, int c, unsigned len);

#endif
Loading